question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useEffect firing in children before parent

See original GitHub issue

Do 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:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
dycommented, Apr 5, 2019

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. in App:

import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { navigate } from "hookrouter";

import "./styles.css";

function App() {
  let auth = useStore('auth')
  useEffect(() => {
    console.log("startup");
    if (!auth.tokens) navigate('/sign-in')
    // ...restore saved session, redirects etc.
  }, []);

  return <Content />;
}

function Content() {
  useEffect(() => {
    console.log("init data");
    // ...fetch content etc.
  }, []);

  return <></>;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

But the children effects run before the App. What would be the right way to organize startup?

@gaearon @mpgon

6reactions
gaearoncommented, Apr 2, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found