reactjs - Confused about execution order -
in react app, have app.jsx
component in perform basic functions such getting window size, read user info calling api function, etc.
this top level component @ entry point. entry point looks like:
render ( <provider store={store}> <app> <homepagecomponent /> </app> </provider>, document.getelementbyid('app') );
what i'm confused in home page component, have bunch of sub components. looks componentdidmount()
function of sub component inside home page executing before componentdidmount()
function of app
component.
is way supposed work? inside out? putting basic house keeping functions getting user info inside app
component thinking execute first.
yes, expected behaviour. there's discussion in this issue.
some highlights:
if componentdidmount executed parent-first, thing you'd find there node, because child nodes have not been mounted moment. makes componentdidmount useless majority of use cases (e.g. counting offsetwidth, attaching third-party non-react plugins, etc). (gaearon)
it's not bug maybe confusing docs. it's creation order of dom elements. has nothing being in document or not, since cannot guarantee anyway (since root may out of document). (sebmarkbage)
technically life-cycle methods guarantee particular component mounted. doesn't make promises regard it's children. relying on order indication you're breaking encapsulation boundaries of components. (sebmarkbage)
Comments
Post a Comment