Skip to content

Instantly share code, notes, and snippets.

@whoisYeshua
Created April 13, 2024 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisYeshua/649f0f63a02e283f60d50725c654297a to your computer and use it in GitHub Desktop.
Save whoisYeshua/649f0f63a02e283f60d50725c654297a to your computer and use it in GitHub Desktop.
React Reconciliation
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 butonElement = <MyButton />;
return (
<div>
<button onClick={forceUpdate}>forceUpdate</button>
<button onClick={() => setFlag((prevFlag) => !prevFlag)}>change</button>
{flag ? (
<div>
{butonElement}
</div>
) : (
<section>
{butonElement}
</section>
)}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment