Created
May 19, 2020 15:26
-
-
Save ozcanzaferayan/2e5e148df189d8a394e2cc4c20be00db to your computer and use it in GitHub Desktop.
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 React, { useState } from "react"; | |
import { useRecoilState, atom } from "recoil"; | |
const todoListState = atom({ | |
key: "todoListState", | |
default: ["Apples", "Eggs", "Butter"], | |
}); | |
function App() { | |
const [todoList, setTodoList] = useRecoilState(todoListState); | |
const [todo, setTodo] = useState(""); | |
const deleteItemAt = (index) => { | |
const todos = [...todoList]; | |
todos.splice(index, 1); | |
setTodoList(todos); | |
}; | |
return ( | |
<> | |
<input value={todo} onChange={(e) => setTodo(e.target.value)} /> | |
<button onClick={() => setTodoList((todos) => [...todos, todo])}> | |
Add | |
</button> | |
{todoList.map((item, index) => ( | |
<li key={item}> | |
{item} | |
<button onClick={() => deleteItemAt(index)}>Delete</button> | |
</li> | |
))} | |
</> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment