Last active
May 8, 2022 04:03
-
-
Save Kelin2025/7bac2f74c00852104fbb1d85ea743741 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { createStore, createEvent } from 'effector' | |
// Create events | |
const inputChanged = createEvent('Input changed') | |
const todoAdded = createEvent('Todo added') | |
const todoRemoved = createEvent('Todo removed') | |
const todoToggled = createEvent('Todo toggled') | |
const $todo = createStore('') | |
const $todos = createStore([]) | |
const $todosCount = $todos.map(todos => todos.length) | |
const $completedTodos = $todos.map(todos => todos.filter(todo => todo.completed)) | |
// Stores have .on method | |
// store.on(event, (state, payload) => newState) | |
$todo | |
.on(inputChanged, (state, value) => value) | |
.on(todoAdded, () => '') | |
$todos | |
.on(todoAdded, (todos, { text }) => [...todos, ({ text, completed: false })]) | |
.on(todoRemoved, (todos, idx) => todos.filter((_, curIdx) => curIdx !== idx)) | |
.on(todoToggled, (todos, idx) => { | |
return todos.map((todo, curIdx) => { | |
if (curIdx === idx) { | |
return { ...todo, completed: !todo.completed } | |
} else { | |
return todo | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you explain where
{text}
comes from on line 22?