How can I reset a react component including all transitively reachable state?

前端 未结 3 562
无人共我
无人共我 2020-12-02 10:37

I occasionally have react components that are conceptually stateful which I want to reset. The ideal behavior would be equivalent to removing the old component and readding

3条回答
  •  盖世英雄少女心
    2020-12-02 10:38

    To ensure that the implicit browser state you mention and state of children is reset, you can add a key attribute to the root-level component returned by render; when it changes, that component will be thrown away and created from scratch.

    render: function() {
        // ...
        return 
    {children}
    ; }

    There's no shortcut to reset the individual component's local state.

提交回复
热议问题