Created
January 24, 2022 01:56
-
-
Save ynwd/2b562eed3812f3bf6cffdbe7e9fd06ba to your computer and use it in GitHub Desktop.
react memo
This file contains 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
import { useState, memo } from "react" | |
function todos({ todos }) { | |
console.log("child render") | |
return ( | |
<> | |
<h2>My Todos</h2> | |
{todos.map((todo, index) => { | |
return <p key={index}>{todo}</p> | |
})} | |
</> | |
) | |
} | |
const Todos = memo(todos) | |
const App = () => { | |
const [count, setCount] = useState(0) | |
const [todos, setTodos] = useState(["todo 1", "todo 2"]) | |
const increment = () => { | |
setCount((c) => c + 1) | |
} | |
return ( | |
<> | |
<Todos todos={todos} /> | |
<hr /> | |
<div> | |
Count: {count} | |
<button onClick={increment}>+</button> | |
</div> | |
</> | |
) | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment