Skip to content

Instantly share code, notes, and snippets.

@JaysonChiang
Last active February 26, 2021 15:18
Show Gist options
  • Save JaysonChiang/fd4c4a2c2fcfc9a689b14d5599f9f190 to your computer and use it in GitHub Desktop.
Save JaysonChiang/fd4c4a2c2fcfc9a689b14d5599f9f190 to your computer and use it in GitHub Desktop.
State with React Component
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