Skip to content

Instantly share code, notes, and snippets.

@abelsan
Created June 12, 2020 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsan/709d5a9bcc2571c9a41533e4ffc251cf to your computer and use it in GitHub Desktop.
Save abelsan/709d5a9bcc2571c9a41533e4ffc251cf to your computer and use it in GitHub Desktop.
Hello World for react hooks
import React, { useState } from 'react';
import './App.css';
function Todo({todo,index}){
return <div className="todo">{todo.text}</div>
}
function TodoForm({addTodo}){
const [value,setValue] = useState('');
const handleSubmit = e => {
e.preventDefault();
if(!value) return;
addTodo(value);
setValue('');
}
return(
<form onSubmit={handleSubmit}>
<input
type="text"
className="input"
value={value}
placeholder="Add Todo..."
onChange={e => setValue(e.target.value)} />
</form>
)
}
function App(){
const [todos, setTodos] = useState([
{
text: 'learn react',
isCompleted: false
},
{
text: 'meet friend for lunch',
isCompleted: false
},
{
text: 'build todo app',
isCompleted: false
}
]);
const addTodo = text => {
const newTodos = [...todos, { text }];
setTodos(newTodos);
}
return (
<div className="app">
<div className="todo-list">
{todos.map((todo,index) => (
<Todo key={index} index={index} todo={todo}/>
))}
<TodoForm addTodo={addTodo} />
</div>
</div>
)
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment