Let’s say you’re writing a stateful, reusable, react component named Foo.
John Owen
11

Thanks for your feedback. About your question the more simple way you can do that is passing a prop from the parent and restoring your initial state in the component lifecycle method componentWillReceiveProps via setState, which won’t trigger any additional re-render due to React batches the updates. My concern is that this way you’ll probably have duplicated source of truth, derivative state from props and it should be avoided whenever possible, but if you know the tradeoffs it’s fine.

The complex solution is making your component a higher order component and pass via props to your decorated components a reset() function that you can call whenever you like. The implementation of reset() function is no more than a setState() restoring the initial state. This way it’s more robust but more complex, you have to figure out how to compose properly your components given your requirements.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.