All Projects → Eliav2 → how-react-hooks-work

Eliav2 / how-react-hooks-work

Licence: other
Understand how React-hook really behaves, once and for all!

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to how-react-hooks-work

react-admin-nest
React和Ant Design和 Nest.js 和 Mysql 构建的后台通用管理系统。持续更新。
Stars: ✭ 123 (+68.49%)
Mutual labels:  react-components, react-hooks
MetFlix
A Movie app demo. Like NetFlix ❤️
Stars: ✭ 50 (-31.51%)
Mutual labels:  react-components, react-hooks
tiny-ui
⚛️ A friendly UI component set for React.js
Stars: ✭ 202 (+176.71%)
Mutual labels:  react-components, react-hooks
frontend-toolbox
Frontend tools which we used in snappmarket v2
Stars: ✭ 37 (-49.32%)
Mutual labels:  react-components, react-hooks
react-remark
React component and hook to use remark to render markdown
Stars: ✭ 81 (+10.96%)
Mutual labels:  react-components, react-hooks
mint-ui
Design System | React UI components for web
Stars: ✭ 17 (-76.71%)
Mutual labels:  react-components, react-hooks
fluent-windows
🌈 React components that inspired by Microsoft's Fluent Design System.
Stars: ✭ 122 (+67.12%)
Mutual labels:  react-components, react-hooks
awesome-web-react
🚀 Awesome Web Based React 🚀 Develop online with React!
Stars: ✭ 31 (-57.53%)
Mutual labels:  react-components, react-hooks
furl
Functional react.js components.
Stars: ✭ 33 (-54.79%)
Mutual labels:  react-components, react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+36.99%)
Mutual labels:  react-components, react-hooks
React-Netflix-Clone
A Fully Responsive clone of Netflix website built using React.JS as a Front-end & Firebase as a Back-end.
Stars: ✭ 91 (+24.66%)
Mutual labels:  react-components, react-hooks
react-native-value-picker
Cross-Platform iOS(ish) style picker for react native.
Stars: ✭ 18 (-75.34%)
Mutual labels:  react-components, react-hooks
next-qrcode
React hooks for generating QRCode for your next React apps.
Stars: ✭ 87 (+19.18%)
Mutual labels:  react-components, react-hooks
hx
A simple, easy to use library for React development in ClojureScript.
Stars: ✭ 244 (+234.25%)
Mutual labels:  react-components, react-hooks
react-use-hubspot-form
Embed HubSpot forms into your React components using hooks! Works with Create React App, Gatsby and other platforms.
Stars: ✭ 41 (-43.84%)
Mutual labels:  react-components, react-hooks
Nectus
A boilerplate Flask API for a Fullstack Project with some additional packages and configuration prebuilt. ⚙
Stars: ✭ 32 (-56.16%)
Mutual labels:  react-components, react-hooks
Primereact
The Most Complete React UI Component Library
Stars: ✭ 2,393 (+3178.08%)
Mutual labels:  tree, react-components
React Vtree
React component for efficiently rendering large tree structures
Stars: ✭ 185 (+153.42%)
Mutual labels:  tree, react-components
react-guidebook
📚 React 知识图谱 关于概念、技巧、生态、前沿、源码核心
Stars: ✭ 22 (-69.86%)
Mutual labels:  react-components, react-hooks
react-sample-projects
The goal of this project is to provide a set of simple samples, providing and step by step guide to start working with React.
Stars: ✭ 30 (-58.9%)
Mutual labels:  react-components, react-hooks

How React hooks work - in depth

Last update: 10.3.22

(please note few fixes were made - so if you came from another mirror note that all other mirrors contain inaccuracies)

React hooks image

TLDR;

have some experience with React and React Hooks, and prefer fewer words and more code? jump over to the Examples section. if something is not clear you can come back and re-read the explanations.

if this is not the case - please continue from here.

Intro

In simple cases, React Hooks will magically do exactly what you meant for, but in other cases, their behavior can feel inconsistent and unpredictable. the next article will try to deeply explain and demonstrate React hooks behavior.

Note: this article does not aim to teach all the different hooks available in React, or how to use them - its main purpose is a deep understanding of what actually happens when you call React hook(like useState or useEffect).

This article consists of four main sections:

  • Definitions - this section summarizes important terms in React and web development which necessary to understand the rest of the article.
  • React Hooks - explains what type of hooks exists, what the difference between them, and how they behave.
  • Examples - examples that demonstrate everything explained in this article with an increasing difficulty rate.
  • Recap - summary with the most important notes.

The article is not for starters, and I will assume that you have some experience with React and React hooks.

please feel free to re-read sections, and ask your best friend Google when it's needed. also, feel free to ask questions on discussion in the repo if you really feel stuck.

Links

article mirrors

All mirrors(at medium, Reddit, etc) are considered deprecated as I don't update them when needed. please read from the Home page

Definitions

the more important definitions here are: render, update, React hook and phase.

  • browser DOM - a tree of HTML elements. These elements make up everything the user sees in the browser, including this very page.
  • React - A library for manipulating React components.
  • React component - function(or class) that holds stateful logic managed by React lib, that component usually returns UI elements based on the stateful logic of the same component. React have class components, and functional components(FC).
  • Stateful logic/state - variables that hold data of the current state of the component. this data can be changed over time based on events or usage. these variables are stored and managed by React lib(it means for example that when you wish to change the state you will ask React to do it by using setState and React would schedule this change, you cannot directly change these values).
  • React tree - a tree of React components(like the tree you can see in React devtools), which is managed and used internally by React. this is not the same as the browser's DOM tree(however, it will later help React create and update the DOM tree on each render).
  • React renderer - ReactDOM in web(or React-native in mobile) - a library that knows how to manipulate React tree and 'render' it into the browser's DOM in the desired location(in React apps usually to root element). The renderer manages a Virtual DOM (VDOM) which is created and updated based on the given React tree.
  • phase - this is not an official term, I'm using this term in this tutorial to describe a certain point of time in a React component. update: also React calls this phase.
  • render - Explained in detail later in Render cycle section.
  • update phase - in this article when we say that a component 'updates', we are saying that the function component body re-executed. this is a the first step of a render cycle.
  • Effect phase - this is the name of the phase after the render ends and the UI is fully updated, React let as execute callbacks on this phase by using useEffect, useLayoutEffect on function components or componentDidMount and componentDidUpdate on class components.
  • React hook - A primitive that shares stateful logic with the parent Component. The hook and the parent component updates are triggers in the same phase, and the effects of the hook and the FC also fire in the same phase(demonstrated later). React hook is allowed to be called only at the top level of FC. the reason for that is because internally React relies on the order in which Hooks are called.

Note - These definitions were summarized by me and may not be accurate, but they are sufficient to understand the rest of the article

React Hooks

There 2 types of React hooks:

  • State hooks - like useState or useReducer. these hooks use and possibly manipulates the parent component stateful logic.
  • Effect hooks - one of useEffect or useLayoutEffect. these hooks receive a callback function and usually a dependency array. the callback function will be scheduled by React to fire on a later phase(see definition above). the exact phase is dependent on the effect that was chosen.
    Effects from the same type will be executed in the order of declaration.

Render cycle

So what is a render?

  • when it happens - a 'render' will occur on a React component as a result of an event that changed the state of this component. the render will propagate down to each child of this component and will trigger a render recursively on them as well. for example - onClick event which called setState. another example is the 'Mount' event, which will trigger a render on a component and any of his children recursively on the first render.
  • what it means - when a 'render' is triggered on a React component - the Component body would be re-executed(or the component render method on class component), this will also execute the function body recursively on each child. At the end of these operations - we get an updated React-tree of this component and all of Its children. this React-tree is passed to the React renderer and the renderer would update the sub dom-tree of this component if needed.

phases of render

  • render:
    • useLayoutEffect cleanup from previous render
    • useEffect cleanup from previous render
    • construction of sub React-tree by recursively calling the React component function body (or the render method on class components)- in this article we call this update phase.
    • passing this React-tree to the renderer that will figure out what's sections of the DOM needs to be updated.
  • commit:
    • the renderer updates the DOM using React’s “diffing” algorithm.
    • now the browser DOM is fully updated in memory but the browser has not painted it to the UI(the event loop has not yet ended). means that any access to the DOM here will get the updated DOM properties(such location and dimensions), but changes has not flushed to the UI.
    • useLayoutEffect is now called.
    • javascript event loop has ended, and the browser paints the updated DOM(the UI is fully updated).
    • useEffect is now called(asynchronously).

Note

  • the cleanup effect will never fire on the first render(because there is no prior effect to cleanup from).
  • when component unmount only the cleanup effect are fired.

Render cycle summary:

per render cycle: Each effect fires the most 1 time, excluding update call which fires at least once.

The effects are fired in this order(excluding the first render), and only if was scheduled:

  1. update - may be called several times for a single render, and will occur one after another before any effect!
  2. useLayoutEffects cleanup
  3. useLayoutEffects
  4. useEffects cleanup
  5. useEffects

the order on the first render:

  1. update (possibly multiple times)
  2. useLayoutEffects
  3. useEffects

the order when component unmount(this is not exactly a 'render'):

  1. useLayoutEffect cleanup
  2. useEffect cleanup

the AllPhases example demonstrates this very well.

Super Important Notes

  • Calling state hook from FC body will cause React to schedule another update call. see example
  • Calling state hook from effect(like useEffect or useLayoutEffect) will cause React to schedule another render. see example

Examples

The latest examples are the most interesting, but to understand them one has to understand the first examples first, so make sure to follow the examples one after another.

important Note - each line of the code that will come next is part of the tutorial, even the comments. read them all to follow along.

Basic

OK enough words. see the next example.

const Basic = () => {
  // log function helper
  // this will help up follow the component phase cycle
  const render = useRef(0);
  const call = useRef(0);
  const consoleState = () => `{call:${call.current},render:${render.current}}`;
  const log = (...args) => console.log(...args, consoleState());
  // update phase counts
  call.current += 1;
  useEffect(() => {
    render.current += 1;
  });

  //logic
  useEffect(() => {
    log("mount has finished");
  }, []);
  useEffect(() => {
    log("render has finished");
  });
  log("update call");
  return <div />;
};

what order of logs would you expect when the component mounts? think for a second and replace the '?':

/**
 * expected logs:
 *    update call           {call:?,render:?}
 *    mount has finished    {call:?,render:?}
 *    render has finished   {call:?,render:?}
 */
Reveal answer

well, the order is:

/**
 * expected logs:
 *    update call {call:1,render:0}
 *    mount has finished {call:1,render:1}
 *    render has finished {call:1,render:1}
 */

as we explained earlier, the function body fire first and then the effects.

Code Sandbox

BasicReverse

what will happen if we will replace the effects, does the order will change?

const BasicReverse = () => {
  // log function helper
  // ...
  // logic
  useEffect(() => {
    log("render has finished");
  });
  useEffect(() => {
    log("mount has finished");
  }, []);
  log("update call");
  return <div />;
};

well, the order does change, and will be:

/**
 * expected logs:
 *    update call {call:1,render:0}
 *    render has finished {call:1,render:1}
 *    mount has finished {call:1,render:1}
 */

this is because effect hooks from the same type(here useEffect) are scheduled by React for the same phase and will be executed in the order of declaration, this is a common mistake to think that useEffect with an empty dependency array will fire on the mount and on a different phase from useEffect with no dependency array.

Code Sandbox

useLog

now let's create a log helper hook useLog that will let us keep track of the component phase for later examples:

const useLog = (componentName = "", effect = useEffect) => {
  // keep track of phase
  const render = useRef(0);
  const call = useRef(0);

  const consoleState = () => `{call:${call.current},render:${render.current}}(${componentName})`;
  const log = (...args) => console.log(...args, consoleState());

  effect(() => {
    render.current += 1;
  });
  call.current += 1;

  return log;
};

render.current and call.current will 'tick' at the same rate as the parent component because of hooks natures.
This is simplified useLog, you will see different useLog hook in the UseLog.js file which includes some logic for monitoring execution time.

and usage:

const Basic = () => {
  const log = useLog();
  useEffect(() => {
    log("finished render");
  });
  return <div />;
};

/**
 * expected logs:
 *    finished render {call:1,render:1}()
 */
Code Sandbox

Unmount

if we will trigger unmount after mount the order of the logs will be:

const BasicUnmount = () => {
  const log = useLog();
  useEffect(() => {
    log("mount");
    return () => log("unmount");
  }, []);
  useEffect(() => {
    log("render");
    return () => log("un-render");
  });
  log("update call");
  return <div>asd</div>;
  /**
   * expected logs:
   *    update call {call:1,render:0}
   *    mount {call:1,render:1}
   *    render {call:1,render:1}
   *    unmount {call:1,render:1}
   *    un-render {call:1,render:1}
   */
};

when a component goes through unmounting step - the update phase does not happen, only the effect fire, in the order of declaration.

Code Sandbox

Effect vs LayoutEffect

useLayoutEffect is executed, then useEffect:

const EffectVsLayoutEffect = () => {
  const log = useLog("effects", undefined, "abs");
  useEffect(() => {
    log("useEffect!");
  });
  useLayoutEffect(() => {
    log("useLayoutEffect!");
  });
  return <div />;
  /**
   * expected logs:
   * useLayoutEffect! {call:1,render:0}(effects) 164.565ms
   * useEffect! {call:1,render:1}(effects) 174.52ms
   */
};
Code Sandbox

AllPhases

This demonstrates all the different phases combined. after mount another dummy re-render is scheduled, we will use absolute timing for this example to see when each phase is executed:

const AllPhases = () => {
  const log = useLog("AllPhases", useEffect, "abs");

  const [, setState] = useState({});
  const forceRender = () => setState({});

  useEffect(() => {
    log("useEffect");
    return () => log("useEffect cleanup");
  });
  useLayoutEffect(() => {
    log("useLayoutEffect");
    return () => log("useLayoutEffect cleanup");
  });
  log("update");

  // fire only on mount
  useEffect(() => {
    log("component fully mounted and render cycle ended. now scheduling another render...");
    forceRender();
    return () => log("unmount cleanup");
  }, []);

  return <div />;
  /**
   * expected logs:
   *    update {call:1,render:0}(AllPhases) 146.36ms
   *    useLayoutEffect {call:1,render:0}(AllPhases) 150.345ms
   *    useEffect {call:1,render:1}(AllPhases) 159.425ms
   *    component fully mounted and render cycle ended. now scheduling another render... {call:1,render:1}(AllPhases) 159.71ms
   *    update {call:2,render:1}(AllPhases) 162.05ms
   *    useLayoutEffect cleanup {call:2,render:1}(AllPhases) 163.75ms
   *    useLayoutEffect {call:2,render:1}(AllPhases) 164.34ms
   *    useEffect cleanup {call:2,render:1}(AllPhases) 167.435ms
   *    useEffect {call:2,render:2}(AllPhases) 168.105ms
   *
   * when unmount(move to other page for example):
   *    useLayoutEffect cleanup {call:2,render:2}(AllPhases) 887.375ms
   *    useEffect cleanup {call:2,render:2}(AllPhases) 892.055ms
   *    unmount cleanup {call:2,render:2}(AllPhases) 892.31ms
   */
};

this example deeply demonstrates all the different possible phases while a component renders. make sure you understand that before proceeding to the next examples.

Code Sandbox

UpdateCycle

when you set a state while in the update phase another update phase will be scheduled by React. let's try to force React to trigger 10 update calls before rendering.

const UpdateCycle = () => {
  const log = useLog("UpdateCycle");
  const [, setState] = useState({});
  const forceUpdate = () => setState({});
  const updateCalls = useRef(0);

  const HandleClick = () => {
    updateCalls.current = 0;
    forceUpdate();
  };
  updateCalls.current += 1;
  if (updateCalls.current < 10) forceUpdate();

  useEffect(() => {
    log("render");
  });
  log("update");

  return (
    <div style={boxStyle} onClick={HandleClick}>
      click
    </div>
  );
  /**
   * update {call:1,render:0}(UpdateCycle) 0.33ms
   * update {call:2,render:0}(UpdateCycle) 0.17ms
   * update {call:3,render:0}(UpdateCycle) 0.03ms
   * update {call:4,render:0}(UpdateCycle) 0.025ms
   * update {call:5,render:0}(UpdateCycle) 0.045ms
   * update {call:6,render:0}(UpdateCycle) 0.04ms
   * update {call:7,render:0}(UpdateCycle) 0.03ms
   * update {call:8,render:0}(UpdateCycle) 0.02ms
   * update {call:9,render:0}(UpdateCycle) 0.03ms
   * update {call:10,render:0}(UpdateCycle) 0.015ms
   * render {call:10,render:1}(UpdateCycle) 0.245ms
   */
};

as we can see, we forced React to re-call the function body 10 times before performing the render. we can also notice that the render phase occurred 0.245ms after the last update call.

Code Sandbox

RenderCycle

Ok, so we saw what happens when we update the state while in the update phase, but what happens if we try to update the state when we are no longer in the update phase? well, React will schedule an entire re-render cycle for the component. each render cycle will also include at least one update call.
let's force 5 render cycles:

const RenderCycle = () => {
  const log = useLog("RenderCycle");
  const [, setState] = useState({});
  const forceRender = () => setState({});
  const renderCalls = useRef(0);

  const HandleClick = () => {
    renderCalls.current = 0;
    forceRender();
  };

  useEffect(() => {
    renderCalls.current += 1;
    if (renderCalls.current < 5) forceRender();
    log("render");
  });
  log("update");

  return (
    <div style={boxStyle} onClick={HandleClick}>
      click
    </div>
  );
  /**
   * update {call:1,render:0}(RenderCycle) 0.365ms
   * render {call:1,render:1}(RenderCycle) 0.33ms
   * update {call:2,render:1}(RenderCycle) 0.26ms
   * render {call:2,render:2}(RenderCycle) 0.315ms
   * update {call:3,render:2}(RenderCycle) 0.12ms
   * render {call:3,render:3}(RenderCycle) 0.25ms
   * update {call:4,render:3}(RenderCycle) 0.07ms
   * render {call:4,render:4}(RenderCycle) 0.495ms
   * update {call:5,render:4}(RenderCycle) 0.055ms
   * render {call:5,render:5}(RenderCycle) 0.135ms
   */
};

we can see that each render cycle comes with an update call.

Code Sandbox

CombinedCycle

now let's say we want 5 update calls for each render. let's force 3 renders:

const CombinedCycle = () => {
  const log = useLog("CombinedCycle");
  const [, setState] = useState({});
  const forceUpdate = () => setState({});
  const updateCalls = useRef(0);
  const renderCalls = useRef(0);

  const HandleClick = () => {
    updateCalls.current = 0;
    renderCalls.current = 0;
    forceUpdate();
  };
  updateCalls.current += 1;
  if (updateCalls.current < 5) forceUpdate();

  useEffect(() => {
    renderCalls.current += 1;
    if (renderCalls.current < 3) forceUpdate();
    updateCalls.current = 0;
    log("render");
  });
  log("update");

  return (
    <div style={boxStyle} onClick={HandleClick}>
      click
    </div>
  );
};
/**
 * update {call:1,render:0}(CombinedCycle) 0.085ms
 * update {call:2,render:0}(CombinedCycle) 0.17ms
 * update {call:3,render:0}(CombinedCycle) 0.03ms
 * update {call:4,render:0}(CombinedCycle) 0.025ms
 * update {call:5,render:0}(CombinedCycle) 0.03ms
 * render {call:5,render:1}(CombinedCycle) 0.29ms
 * update {call:6,render:1}(CombinedCycle) 0.03ms
 * update {call:7,render:1}(CombinedCycle) 0.095ms
 * update {call:8,render:1}(CombinedCycle) 0.02ms
 * update {call:9,render:1}(CombinedCycle) 0.04ms
 * update {call:10,render:1}(CombinedCycle) 0.025ms
 * render {call:10,render:2}(CombinedCycle) 0.08ms
 * update {call:11,render:2}(CombinedCycle) 0.055ms
 * update {call:12,render:2}(CombinedCycle) 0.085ms
 * update {call:13,render:2}(CombinedCycle) 0.025ms
 * update {call:14,render:2}(CombinedCycle) 0.03ms
 * update {call:15,render:2}(CombinedCycle) 0.03ms
 * render {call:15,render:3}(CombinedCycle) 0.085ms
 */
Code Sandbox

MultipleComponents

Let's combine the last 3 examples into the common parent.

import UpdateCycle from "./UpdateCycle";
import RenderCycle from "./RenderCycle";
import CombinedCycle from "./CombinedCycle";

const Example = () => (
  <>
    <UpdateCycle />
    <RenderCycle />
    <CombinedCycle />
  </>
);

now stop. think. what would you expect? does each component will go through its own update-render phases or maybe the update calls will occur one after another and then the effects one after another?

Reveal answer

the entire tree goes through the phase of the update, and only then the effects are fired.

/**
 * update {call:1,render:0}(UpdateCycle) 0.505ms
 * update {call:2,render:0}(UpdateCycle) 0.22ms
 * update {call:3,render:0}(UpdateCycle) 0.03ms
 * update {call:4,render:0}(UpdateCycle) 0.035ms
 * update {call:5,render:0}(UpdateCycle) 0.075ms
 * update {call:6,render:0}(UpdateCycle) 0.05ms
 * update {call:7,render:0}(UpdateCycle) 0.04ms
 * update {call:8,render:0}(UpdateCycle) 0.04ms
 * update {call:9,render:0}(UpdateCycle) 0.045ms
 * update {call:10,render:0}(UpdateCycle) 0.025ms
 * update {call:1,render:0}(RenderCycle) 0.035ms
 * update {call:1,render:0}(CombinedCycle) 0.065ms
 * update {call:2,render:0}(CombinedCycle) 0.06ms
 * update {call:3,render:0}(CombinedCycle) 0.065ms
 * update {call:4,render:0}(CombinedCycle) 0.045ms
 * update {call:5,render:0}(CombinedCycle) 0.04ms
 * render {call:10,render:1}(UpdateCycle) 0.15ms
 * render {call:1,render:1}(RenderCycle) 0.33ms
 * render {call:5,render:1}(CombinedCycle) 0.17ms
 * update {call:2,render:1}(RenderCycle) 0.295ms
 * update {call:6,render:1}(CombinedCycle) 0.045ms
 * update {call:7,render:1}(CombinedCycle) 0.045ms
 * update {call:8,render:1}(CombinedCycle) 0.04ms
 * update {call:9,render:1}(CombinedCycle) 0.06ms
 * update {call:10,render:1}(CombinedCycle) 0.04ms
 * render {call:2,render:2}(RenderCycle) 0.145ms
 * render {call:10,render:2}(CombinedCycle) 0.145ms
 * update {call:3,render:2}(RenderCycle) 0.055ms
 * update {call:11,render:2}(CombinedCycle) 0.05ms
 * update {call:12,render:2}(CombinedCycle) 0.085ms
 * update {call:13,render:2}(CombinedCycle) 0.03ms
 * update {call:14,render:2}(CombinedCycle) 0.015ms
 * update {call:15,render:2}(CombinedCycle) 0.02ms
 * render {call:3,render:3}(RenderCycle) 0.125ms
 * render {call:15,render:3}(CombinedCycle) 0.075ms
 * update {call:4,render:3}(RenderCycle) 0.06ms
 * render {call:4,render:4}(RenderCycle) 0.135ms
 * update {call:5,render:4}(RenderCycle) 0.025ms
 * render {call:5,render:5}(RenderCycle) 0.06ms
 */
Code Sandbox

phew! that was tough. if you read and understand everything to this point you can confidently say that you understand React hook's nature.

Component with complicated lifecycle

so why do we need to understand all of this? well, in simple cases you don't, but when dealing with a component with a complicated lifecycle you can sometimes get confused by the component's behavior. an example of such a component will be react-xarrow which needs to trigger callback on different phases to get the right dimensions and activate animations callbacks on different phases. by writing this lib I learned how hooks really behave, so I could optimize the render cycle and improve performance by far.
Pro-tip: with components with a complicated lifecycle you would probably want to use many times useRef and not useState! this way you don't force re-renders during updates and this solves problems with state variables that depend on other state variables which will be 'ready' only on the next render.

Recap

  • on each phase: An entire React Tree goes through each phase in a render cycle one after another, which means that if one component in the tree is in the useEffect phase, for example, all the different components in the tree are currently also in the useEffect phase.
  • for a React Tree: on the same phase, each Component on React tree will fire each phase in the same order of the declaration of the react component in the React tree. for example:
    <>
      <Comp1 />
      <Comp2 />
    </>
    the useEffect if Comp1 will fire and only then the useEffect Comp2 will fire.
  • On the same React component: on the same phase, each effect from the same type will fire in the order of declaration.

That's it! you now understand what really going on when you ask React to update some state a React component.

If you liked this tutorial make sure to like it and share it! thank you for reading until the end!

<style> details { border: 1px solid #aaa; border-radius: 4px; padding: .5em .5em 0; margin: 1em 0; } </style>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].