Skip to content

Instantly share code, notes, and snippets.

@Kelin2025
Last active May 8, 2022 04:03
Show Gist options
  • Save Kelin2025/7bac2f74c00852104fbb1d85ea743741 to your computer and use it in GitHub Desktop.
Save Kelin2025/7bac2f74c00852104fbb1d85ea743741 to your computer and use it in GitHub Desktop.
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
}
})
})
@Coder2012
Copy link

Can you explain where {text} comes from on line 22?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment