Skip to content

Instantly share code, notes, and snippets.

@hnrchrdl
Last active August 18, 2019 17:54
Show Gist options
  • Select an option

  • Save hnrchrdl/76e6f6df7607a51dba1e25485071d404 to your computer and use it in GitHub Desktop.

Select an option

Save hnrchrdl/76e6f6df7607a51dba1e25485071d404 to your computer and use it in GitHub Desktop.
Example 7
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 = useMemo(() => <button onClick={increment}>++</button>, [
increment
]);
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