Skip to content

Instantly share code, notes, and snippets.

@ynwd
Created January 24, 2022 01:56
Show Gist options
  • Save ynwd/2b562eed3812f3bf6cffdbe7e9fd06ba to your computer and use it in GitHub Desktop.
Save ynwd/2b562eed3812f3bf6cffdbe7e9fd06ba to your computer and use it in GitHub Desktop.
react memo
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