React - How to force a function component to render?

前端 未结 7 512
眼角桃花
眼角桃花 2020-12-02 18:18

I have a function component, and I want to force it to re-render.

How can I do so?
Since there\'s no instance this, I cannot call this.forceU

相关标签:
7条回答
  • 2020-12-02 18:23

    0 讨论(0)
  • 2020-12-02 18:26

    Disclaimer: NOT AN ANSWER TO THE PROBLEM.

    Leaving an Important note here:

    If you are trying to forceupdate a stateless component, chances are there is something wrong with your design.

    Consider the following cases:

    1. Pass a setter (setState) to a child component that can change state and cause the parent component to re-render.
    2. Consider lifting state up
    3. Consider putting that state in your Redux store, that can automatically force a re-render on connected components.
    0 讨论(0)
  • 2020-12-02 18:31

    Official FAQ ( https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate ) now recommends this way if you really need to do it:

      const [ignored, forceUpdate] = useReducer(x => x + 1, 0);
    
      function handleClick() {
        forceUpdate();
      }
    
    0 讨论(0)
  • 2020-12-02 18:35

    I used a third party library called use-force-update to force render my react functional components. Worked like charm. Just use import the package in your project and use like this.

    import useForceUpdate from 'use-force-update';
    
    const MyButton = () => {
    
      const forceUpdate = useForceUpdate();
    
      const handleClick = () => {
        alert('I will re-render now.');
        forceUpdate();
      };
    
      return <button onClick={handleClick} />;
    };
    
    0 讨论(0)
  • 2020-12-02 18:41

    This can be done without explicitly using hooks provided you add a prop to your component and a state to the stateless component's parent component:

    const ParentComponent = props => {
      const [updateNow, setUpdateNow] = useState(true)
    
      const updateFunc = () => {
        setUpdateNow(!updateNow)
      }
    
      const MyComponent = props => {
        return (<div> .... </div>)
      }
    
      const MyButtonComponent = props => {
        return (<div> <input type="button" onClick={props.updateFunc} />.... </div>)
      }
    
      return (
        <div> 
          <MyComponent updateMe={updateNow} />
          <MyButtonComponent updateFunc={updateFunc}/>
        </div>
      )
    }
    
    0 讨论(0)
  • 2020-12-02 18:44

    Update react v16.8 (16 Feb 2019 realease)

    Since react 16.8 released with hooks, function components are now have the ability to hold persistent state. With that ability you can now mimic a forceUpdate:

    function App() {
      const [, updateState] = React.useState();
      const forceUpdate = React.useCallback(() => updateState({}), []);
      console.log("render");
      return (
        <div>
          <button onClick={forceUpdate}>Force Render</button>
        </div>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
    <div id="root"/>

    Note that this approach should be re-considered and in most cases when you need to force an update you probably doing something wrong.


    Before react 16.8.0

    No you can't, State-Less function components are just normal functions that returns jsx, you don't have any access to the React life cycle methods as you are not extending from the React.Component.

    Think of function-component as the render method part of the class components.

    0 讨论(0)
提交回复
热议问题