Skip to content

Instantly share code, notes, and snippets.

@hnrchrdl
Last active August 18, 2019 16:09
Show Gist options
  • Save hnrchrdl/5da2045c7e72449066cecbc32563faba to your computer and use it in GitHub Desktop.
Save hnrchrdl/5da2045c7e72449066cecbc32563faba 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}
</>
);
};
const Child = ({ 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