React - How to force a function component to render?

前端 未结 7 515
眼角桃花
眼角桃花 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: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.

提交回复
热议问题