Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Classes are meant to have state. The state object is called state.

Except that's not how React works at all. There's no state in the class that lives between renders, it only seems that way due to... magic. The class gets destroyed and created between each render and the state object is automagically recreated with the previous state. You can complain about Hooks but saying they are magical and React classes are not is just wrong.



React classes don’t get destroyed and recreated each render. You can throw a console.log in the constructor, or save variables locally to the class, and see this for yourself.


Set `this.foobar` to something in a React class and you’ll see it persist between renders.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: