Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Created May 19, 2020 15:29
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 ozcanzaferayan/45d20ec6328b7398e495d29afa357ae0 to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/45d20ec6328b7398e495d29afa357ae0 to your computer and use it in GitHub Desktop.
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);
};
const editItemAt = (index) => {
const todos = [...todoList];
var item = prompt("Change item name", todoList[index]);
todos[index] = item;
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={() => editItemAt(index)}>Edit</button>
<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