useEffect firing in children before parent
See original GitHub issueDo you want to request a feature or report a bug? Feature (I believe) What is the current behavior? Right now, the effects in useEffect fire children-first. I know this makes sense, since the behaviour of the first render has a close correlation to cDm. However, I cannot seem to find a way to fire events parent-first. Before, I could do it with cWm, and after that was deprecated, in the constructor. How can I accomplish that with hooks? CodeSandbox example: https://codesandbox.io/s/035lqnozzl?fontsize=14 What is the use case? Imagine I want to post to an external server when two components were first rendered, to measure a sort of meaningful paint.
- Component A -> post("first render"", component: A)
----
--------
------------ Component B -> post("first render", component: B)
How could I accomplish this with hooks?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:14 (4 by maintainers)
Top Results From Across the Web
What is the correct order of execution of useEffect in React ...
Sandbox to give you an idea how useEffect is called, it has Roster as Parent and Schedule as child. if you navigate to...
Read more >Tips for Using React's UseEffect Effectively - Better Programming
1. Child Effects Fire First ... Think of the useEffect Hook as componentDidMount , componentDidUpdate , and componentWillUnmount combined. So the useEffect Hook ......
Read more >Underrated React Hooks you're missing out on - LogRocket Blog
In React, data is passed from parent to child components via props, ... Although React fires both useEffect and useLayoutEffect after ...
Read more >is it possible to ensure useEffect in parent executes only when ...
Hi guys, as the title says, is there a pattern to follow to ensure that I execute useEffect in a parent component only...
Read more >Hooks FAQ - React
What is the prior art for Hooks? ... Often, render props and higher-order components render only a single child. ... See conditionally firing...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can we please elaborate the answer? Facing the same situation https://codesandbox.io/s/0qx6lq4lrn?fontsize=14.
Basically I hoped to run “startup” code, setting up API, managing saved session, redirecting to sign-in if user is not authenticated, connecting store to network events etc., basically setting up App state. First expectation for that is to put
useEffect
at the root level, ie. inApp
:But the children effects run before the App. What would be the right way to organize startup?
@gaearon @mpgon
But that’s not how React works. It won’t somehow paint the child separate from the parent. Maybe you can add an example where it matters? I don’t understand the pattern you’re referring to.