考虑下面的钩子示例

   import { useState } from 'react';

   function Example() {
       const [count, setCount] = useState(0);

       return (
           <div>
               <p>You clicked {count} times</p>
               <button onClick={() => setCount(count + 1)}>
                  Click me
               </button>
          </div>
        );
     }

基本上,我们使用this.forceUpdate()方法强制组件立即在React类组件中重新渲染,如下例所示

    class Test extends Component{
        constructor(props){
             super(props);
             this.state = {
                 count:0,
                 count2: 100
             }
             this.setCount = this.setCount.bind(this);//how can I do this with hooks in functional component 
        }
        setCount(){
              let count = this.state.count;
                   count = count+1;
              let count2 = this.state.count2;
                   count2 = count2+1;
              this.setState({count});
              this.forceUpdate();
              //before below setState the component will re-render immediately when this.forceUpdate() is called
              this.setState({count2: count
        }

        render(){
              return (<div>
                   <span>Count: {this.state.count}></span>. 
                   <button onClick={this.setCount}></button>
                 </div>
        }
 }

但我的问题是,我如何才能强制上述功能组件重新渲染立即与挂钩?


当前回答

const useForceRender = () => {
  const [, forceRender] = useReducer(x => !x, true)
  return forceRender
}

使用

function Component () {
  const forceRender = useForceRender() 
  useEffect(() => {
    // ...
    forceRender()
  }, [])

其他回答

你最好只让你的组件依赖于状态和道具,它会像预期的那样工作,但如果你真的需要一个函数来强制组件重新呈现,你可以使用useState钩子,并在需要时调用这个函数。

例子

const { useState, useEffect } = React; 函数 Foo() { const [, forceUpdate] = useState(); useEffect(() => { setTimeout(forceUpdate, 2000); }, []); 返回 <div>{Date.now()}</div>; } ReactDOM.render(<Foo />, document.getElementById(“root”)); <script src=“https://unpkg.com/react@16.7.0-alpha.0/umd/react.production.min.js”></script> <script src=“https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.production.min.js”></script> <div id=“root”></div>

你可以像这样简单地定义useState:

const [, forceUpdate] = React.useState(0);

和用法:forceUpdate(n => !n)

希望这对你有所帮助!

简单的代码

const forceUpdate = React.useReducer(bool => !bool)[1];

Use:

forceUpdate();

有点晚了,但我注意到大多数(所有)的答案都错过了可以传递回调到forceUpdate生命周期方法的部分。

根据react源代码,此回调具有与setState方法中的行为相同的行为-它在更新后执行。

因此,最正确的实现应该是这样的:

/** *增加导致重传并执行回调的状态 * @param {function}回调-状态更新后执行的回调 * @返回{函数} * / export const useForceUpdate = (callback) => { const [state, updater] = useReducer((x) => x + 1,0); useEffect(() => { Callback && Callback (); },[状态]); 返回useCallback(() => { 更新(); },[]); };

单行解决方案:

const [,forceRender] = useReducer((s) => s+1, 0)

你可以在这里了解useReducer。 https://reactjs.org/docs/hooks-reference.html#usereducer