Skip to content

Instantly share code, notes, and snippets.

@GuilhemBarboux
Created January 12, 2023 15:57
Show Gist options
  • Save GuilhemBarboux/4330e8c1641d657ea294bbf7bf8cd680 to your computer and use it in GitHub Desktop.
Save GuilhemBarboux/4330e8c1641d657ea294bbf7bf8cd680 to your computer and use it in GitHub Desktop.
Test for render order
import React, { FunctionComponent, useEffect, useLayoutEffect } from 'react';
interface OwnChildProps {}
type ChildProps = OwnChildProps;
const Child: FunctionComponent<ChildProps> = props => {
useEffect(() => {
console.log('Child useEffect');
return () => {
console.log('Child release useEffect');
};
}, []);
useLayoutEffect(() => {
console.log('Child useLayoutEffect');
return () => {
console.log('Child release useLayoutEffect');
};
}, []);
console.log('Child render function');
return;
};
interface OwnParentProps {}
type ParentProps = OwnParentProps;
const Parent: FunctionComponent<ParentProps> = props => {
const [active, setActive] = useState(false);
useEffect(() => {
console.log('Parent useEffect');
setActive(true);
return () => {
console.log('Parent release useEffect');
};
}, [active, setActive]);
useLayoutEffect(() => {
console.log('Parent useLayoutEffect');
return () => {
console.log('Parent release useLayoutEffect');
};
}, []);
console.log('Parent render function');
return <Child />;
};
export default Parent;
@GuilhemBarboux
Copy link
Author

Quel est l'ordre du rendu des consoles.log ?

@GuilhemBarboux
Copy link
Author

GuilhemBarboux commented Jan 16, 2023

Render function parent to child

Parent render function
Child render function

Layout effects child to parent

Child useLayoutEffect
Parent useLayoutEffect

Effects child to parent

Child useEffect
Parent useEffect

SetActive => re-render

Parent render function
Child render function

Params of useEffect change : release the effect and re-execute

Parent release useEffect
Parent useEffect

Re-render on parent : only state of parent change, no change for the child

Parent render function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment