Trigger child re-rendering in React.js

后端 未结 5 1043
自闭症患者
自闭症患者 2020-12-24 01:38

The Parent (MyList in my example) component renders an array thru a Child (MyComponent) component. Parent decides to change properties in the array

5条回答
  •  一向
    一向 (楼主)
    2020-12-24 02:28

    You should trigger a re-rendering by calling setState() and giving the new props you want to propagate down. If you really want to force an update you can also call forceUpdate().

    If you look at the examples on this page, you can see that setState is the method used to update and trigger a re-rendering. The documentation is also stating (ahaha!) that clearly.

    In your case I would call forceUpdate.

    EDIT: As Jordan mentioned in the comment, it would be better to store items as part of your state. That way you wouldn't have to call forceUpdate but you would really update the state of your component, thus a regular setState with the updated values would work better.

提交回复
热议问题