Skip to content

Instantly share code, notes, and snippets.

@hnrchrdl
Created August 18, 2019 17:23
Show Gist options
  • Save hnrchrdl/6b77f0bbe155f1cc9158aa6f2a1f72f3 to your computer and use it in GitHub Desktop.
Save hnrchrdl/6b77f0bbe155f1cc9158aa6f2a1f72f3 to your computer and use it in GitHub Desktop.
const App = () => {
const [counter, setCounter] = useState(0);
const [aboveFive, setAboveFive] = useState(false);
const increment = () => {
setCounter(n => n + 1);
};
useEffect(() => {
if (counter > 5) {
setAboveFive(true);
}
}, [counter]);
const button = <button onClick={increment}>++</button>;
return (
<>
<h1>App: {counter}</h1>
<Child isAboveFive={aboveFive}>{button}</Child>
</>
);
};
const Child = React.memo(({ isAboveFive, children }) => {
console.log("child renders");
return (
<>
<h2>Child: {!isAboveFive ? "<= 5" : "> 5"}</h2>
{children}
</>
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment