Re:Crank.js – Write JSX-driven components with functions, pr...
by TomMarius on Saturday April 18, 2020 @02:09AM
(#22905691)
While I tend to agree, IMHO your example is off - it is a "state" property because the class is newly instantiated each time a render is going on, and then the property is magically re-populated for you so you can still treat the class like a class, not because "classes are meant to have state" - you would use class properties directly as your state if that was the case. Hooks are exactly the same magic (the state property is populated the same way as the useState hook works), only accessed differently.
[ Parent
| on HN ]
Re:Crank.js – Write JSX-driven components with functions, pr...
by Kkoala on Saturday April 18, 2020 @07:52AM
(#22907070)
> 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?
Re:Crank.js – Write JSX-driven components with functions, pr...
by TomMarius on Saturday April 18, 2020 @08:32AM
(#22907273)
It'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-element...
Re:Crank.js – Write JSX-driven components with functions, pr...
by zwily on Saturday April 18, 2020 @09:10AM
(#22907460)
The 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.
Re:Crank.js – Write JSX-driven components with functions, pr...
by TomMarius on Saturday April 18, 2020 @10:01AM
(#22907774)
You are right. I tried it here, for others: https://codesandbox.io/s/crazy-bush-cg2i0?file=/src/App.js
Re:Crank.js – Write JSX-driven components with functions, pr...
by nightski on Saturday April 18, 2020 @10:14AM
(#22907875)
Sure that is the way it works now. Is that a guarantee of React though or implicit behavior that could change in the future?