Last active
February 26, 2021 15:18
-
-
Save JaysonChiang/fd4c4a2c2fcfc9a689b14d5599f9f190 to your computer and use it in GitHub Desktop.
State with React Component
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 } from 'react'; | |
const TodoList = () => { | |
const [todo, setTodo] = useState(''); | |
const [todos, setTodos] = useState([]); | |
const onClick = () => { | |
setTodo(''); | |
setTodos([...todos, todo]); | |
}; | |
return ( | |
<div> | |
<input value={todo} onChange={(e) => setTodo(e.target.value)} /> | |
<button onClick={onClick}>Add Todo</button> | |
{todos.map((todo) => (<div>{todo}</div>))} | |
</div> | |
); | |
}; | |
export default TodoList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment