Last active
August 18, 2019 17:54
-
-
Save hnrchrdl/76e6f6df7607a51dba1e25485071d404 to your computer and use it in GitHub Desktop.
Example 7
This file contains hidden or 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
| 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