Last active
August 8, 2024 10:42
-
-
Save whoisYeshua/649f0f63a02e283f60d50725c654297a to your computer and use it in GitHub Desktop.
React Reconciliation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useMemo, useReducer, memo } from "react"; | |
const MyButton = memo(() => { | |
const value = useMemo(() => Math.random(), []); | |
return <button>MyButton {value}</button>; | |
}); | |
export default function MyApp() { | |
const [flag, setFlag] = useState(false); | |
const [, forceUpdate] = useReducer((x) => x + 1, 0); | |
const buttonElement = <MyButton />; | |
return ( | |
<div> | |
<button onClick={forceUpdate}>forceUpdate</button> | |
<button onClick={() => setFlag((prevFlag) => !prevFlag)}>change</button> | |
{flag ? ( | |
<div> | |
{buttonElement} | |
</div> | |
) : ( | |
<section> | |
{buttonElement} | |
</section> | |
)} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment