react icon indicating copy to clipboard operation
react copied to clipboard

Children component mounted before parent compoent.

Open code-artisan opened this issue 10 years ago • 19 comments

Hi there. Here is my page:

image

image

I want get host detail from main view.

I think it's should be Main view mounted... --> Drill view mounted..., but actually, it's Drill view mounted... --> Main view mounted....

code-artisan avatar Dec 26 '15 14:12 code-artisan

As far as I know this is expected. By the time componentDidMount fires you can do DOM manipulation so it makes sense that parent only receives it after children are mounted. That said you can use componentWillMount which fires in the opposite direction, parents first.

gaearon avatar Dec 26 '15 18:12 gaearon

What @gaearon said is true, the child mount events fire before the parents. The lifecycle can be seen here: https://github.com/facebook/react/blob/master/src/renderers/shared/reconciler/ReactCompositeComponent.js#L50

milesj avatar Jan 06 '16 05:01 milesj

Yeah, understand, thank you.

code-artisan avatar Jan 06 '16 05:01 code-artisan

Is there a known way around this? In case we don't want the child to mount before the first parent render call?

JadSayegh avatar Sep 25 '17 06:09 JadSayegh

@JadSayegh I may not be correct, but you could use componentWillMount to set state on a prop that can be persisted to your child component. e.g <ChildComponent active={this.state.active} />; then I assume your child can just return null when not active. Once you parent is mounted and render, then update the active state to true.

locomotif avatar Jan 09 '18 09:01 locomotif

@gaearon componentWillMount is deprecated now. How could I deal with API-method that I want to call before children mounting?

Now I'm using componentDidMount. So my API sequence is: deepest child -> child -> parent. But I need sequence to be: parent -> child -> deepest child.

YozhEzhi avatar May 12 '18 11:05 YozhEzhi

Please explain in more detail why you need this.

If it’s just for some kind of bookkeeping, you can move that logic into constructor. Effectively componentWillMount was nearly equivalent to a constructor.

Doing side effects there is not supported though. It’s hard to say more without seeing what you’re actually trying to do and why.

gaearon avatar May 12 '18 11:05 gaearon

@gaearon Thanks for advice to use constructor! It works.

The case: Our parent component should to make getBaseUrl api request to get path to rest-api server. So, when parents' children are mounting - they should to know about those path to rest-api.

Anyway, you fixed my problem :)

YozhEzhi avatar May 12 '18 12:05 YozhEzhi

On Sat, May 12, 2018 at 7:43 AM YozhEzhi [email protected] wrote:

R@gaearon https://github.com/gaearon Thanks for advice to use constructor! cIt works.

The case: Our Issuer's employer identification number (EIN): 06-1515824 r c component should to make rgetBaseUrl api request to get path to rest-api server.c So, when parents' children are mounting - they should to know about those path to rest-api.

Anyway, you fixed my problem :)

— You are receiving Rthis because you are subscribed to this thread.crc Reply to this email directly, view it on GitHub https://github.com/facebook/react/issues/5737#issuecomment-388552637, or mute the thread https://github.com/notifications/unsubscribe-auth/AJPWRMozBr8ihoiXQWhUgDTelrRyrbYZks5txtjngaJpZM4G7dPA .

rr

Sent from Gmail Mobile

shaggy34 avatar May 12 '18 13:05 shaggy34

@YozhEzhi

I don’t understand why you’d need to rely on order there though. Calculate those paths and pass them down as props. Or use context if you don’t want to pass them down explicitly.

gaearon avatar May 12 '18 13:05 gaearon

hello @gaearon , I actually encountered the same issue.

How could I deal with API-method that I want to call before children mounting?

Now I'm using componentDidMount. So my API sequence is: deepest child -> child -> parent. But I need sequence to be: parent -> child -> deepest child.

As @YozhEzhi mentioned above, I want the sequence to be parent -> child -> deepest child However, the difference is, my issue is not API related but need the window instead.

I'm implementing doubleclick ads, the simplified structure would be:

  • pageComp
    • adComp1
    • adComp2

What I want is to setup the general config in pageComp first, then defineSlot for each adComp

The issue is:

  • window.googletag is only available after componentDidMount
  • config should be setup before doing any other stuff

Do you have any suggestion? Thank you in advance.

sabrinaluo avatar May 29 '18 09:05 sabrinaluo

Question related to this topic

If the children takes longer to load due to e.g. api calls and promises, will the parent then mount as soon as it is ready without waiting for the child to finish first? - and then rerender when the child is finished? Or how does it work?

The link is broken @milesj

Sebastian-Nielsen avatar Sep 01 '18 07:09 Sebastian-Nielsen

In React components don't "wait" for each other. There is a feature planned that addresses this (you might have heard of "suspense") but it's not available yet.

So yes, both the child and parent normally mount immediately. There is no notion of child "finishing" today in React. If you mean that your child returned null or a spinner, that still counts as having rendered.

When the child calls setState later (e.g. when data is ready), the child alone re-renders.

gaearon avatar Sep 01 '18 20:09 gaearon

@gaearon hello! I'm having similar issues I think, but in my case it's happening when I try to fire an action within the useEffect hook defined at the entry point for my application. Lower in the component tree, I'm using the useEffect hook to make API calls that use a crucial piece of data being retrieved in the entry point I described. It seems that I would not want to move the fetching of that crucial piece of data down and into every child component that needs it. Thoughts on this? Am I using an anti-pattern here?

brandonparis avatar May 15 '19 13:05 brandonparis

@Brandonprs Your particular use case has more discussion here: https://github.com/facebook/react/issues/15281

ryansully avatar May 30 '19 20:05 ryansully

Hi @gaearon, Now that we have hooks and move on to adapt more function components But the problem is that there no equivalent hooks for doing constructor

Is it correct? If yes, are you guys planning to make one?

phatNfqAsia avatar Aug 07 '19 17:08 phatNfqAsia

@gaearon componentWillMount is deprecated now. How could I deal with API-method that I want to call before children mounting?

Now I'm using componentDidMount. So my API sequence is: deepest child -> child -> parent. But I need sequence to be: parent -> child -> deepest child.

I am also facing the same issue ,but with hooks, There is no constructor even .

ervikashkumar avatar Apr 20 '20 13:04 ervikashkumar

a live demo

react-parent-child-lifecycle-order

https://33qrr.csb.app/

https://codesandbox.io/s/react-parent-child-lifecycle-order-33qrr

create order


parent constructor
parent WillMount
parent render

child constructor
child WillMount
child render
child DidMount

parent DidMount

destroy order

parent WillUnmount

child WillUnmount

// child unmount

// parent unmount



xgqfrms avatar May 18 '20 03:05 xgqfrms

@phatNfqAsia @ervikashkumar We can use this as a custom constructor hook for one time things

/* useConstructor: to setup things before initial render, means This callBack only happens ONCE and it happens BEFORE the initial render. */

Custom Hook:

export const useConstructor = (callBack = () => { }) => { const hasBeenCalled = useRef(false); if (hasBeenCalled.current) { return; } callBack(); hasBeenCalled.current = true; };

Usage:

useConstructor(() => { //one time thing });

rajeevrocker7 avatar Mar 25 '21 13:03 rajeevrocker7