⚠️ NOTE: This post assumes good familiarity with react. You don't need to be an expert, if you've used it to make UIs before, you're gonna be fine,...
For further actions, you may consider blocking this person and/or reporting abuse
Very interesting article ! But shouldn't currentlyRenderedComponent be a stack ? Because with this implementation (setting the variable to undefined) there is a risk of an observable object being accessed in a parent component after the rendering of a child. Is there something I'm missing ?
Hi Nathan! Thanks for your interest! Perhaps it's me, but I'm not quite sure what you mean : )
By "observable object", do you mean the object wrapped with the
store
function?Yes.
Because currentlyRenderedComponent is a global variable, this can occur :
Then, when the observable object is modified, the parent component is not rendered again. A fix for that is replacing currentlyRenderedComponent by a stack in which we push a component when it starts rendering and we pop it when it's done.
Hi Nathan!
Sorry for the late reply! I understand what you mean now, but as far as I understand react, this is not exactly correct :)
React does not render the tree of components "top-to-bottom" so to speak, but rather creates a virtual-dom data structure which is then rendered to the screen. React contains a "scheduler" which decides when to do the actual rendering.
So, the cycle that you described, would rather look more like:
currentlyRenderedComponent = parent component
.currentlyRenderedComponent === undefined
currentlyRenderedComponent = child component
.Dan Abramov (from the react team) has written a blog post about the react rendering model, which I think describes what I'm trying to say in more depth and perhaps more eloquently :)
Here's the section that is relevant to your question: overreacted.io/react-as-a-ui-runti...
Thank you for your explanations, that is the part I was missing and it's very clear now :)
you're very welcome! 👌