3 ms·
> the class is newly instantiated each time a render is going on No, it's not? Where do you think the state goes, if the instance doesn't hold it?
by Kkoala 6y ago
> the class is newly instantiated each time a render is going on
No, it's not? Where do you think the state goes, if the instance doesn't hold it?
- TomMarius 6y agoIt's stored internally in React. The instance of your component does not hold the state, just a reference to the state object which is repopulated each render (just like useState, it's the exact same mechanism), and after the render is done, the instance is destroyed. More here: https://reactjs.org/blog/2015/12/18/react-components-elements-and-instances.html https://reactjs.org/blog/2015/12/18/react-components-element...
- zwily 6y agoThe instance of the class isn’t destroyed every render... you can keep other state on it too that is persisted between renders, changing it just won’t trigger a render.
- nightski 6y agoSure that is the way it works now. Is that a guarantee of React though or implicit behavior that could change in the future?
- boubiyeah 6y agoIt's just an implementation detail.
- deleted 6y ago[deleted]
- TomMarius 6y agoYou are right. I tried it here, for others: https://codesandbox.io/s/crazy-bush-cg2i0?file=/src/App.js https://codesandbox.io/s/crazy-bush-cg2i0?file=/src/App.js
- exdsq 6y agoI don’t know how this library or React work, but you could pass the state as an argument to itself - then what GP said makes sense.