Skip to content

Instantly share code, notes, and snippets.

View ozcanzaferayan's full-sized avatar
⚛️
Overreacting

Özcan Zafer AYAN ozcanzaferayan

⚛️
Overreacting
View GitHub Profile
<span
onClick={() => completeAt(index)}
style={{ textDecoration: item.isCompleted ? "line-through" : "initial"}}>
{item.name}
</span>
import React, { useState } from "react";
import { useRecoilState, atom } from "recoil";
const todoListState = atom({
key: "todoListState",
default: [
{ name: "Apples", isCompleted: false },
{ name: "Eggs", isCompleted: false },
{ name: "Butter", isCompleted: false },
],
const todoListState = atom({
key: "todoListState",
default: [
{ name: "Apples", isCompleted: false },
{ name: "Eggs", isCompleted: false },
{ name: "Butter", isCompleted: false },
],
});
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 editItemAt = (index) => {
const todos = [...todoList];
var item = prompt("Change item name", todoList[index]);
todos[index] = item;
setTodoList(todos);
};
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);
{todoList.map((item, index) => (
<li key={item}>
{item} <button onClick={() => deleteItemAt(index)}>Delete</button>
</li>
))}
const deleteItemAt = (index) => {
const todos = [...todoList];
todos.splice(index, 1);
setTodoList(todos);
};
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);
<button onClick={() => setTodoList((todos) => [...todos, todo])}>Add</button>