Created
June 8, 2019 20:30
-
-
Save cevr/ed97851bb1cd45e24594e49fd8ecb452 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 { action, createStore, StoreProvider, useStore, useActions } from 'easy-peasy'; | |
// 👇 create your store, providing the model | |
const store = createStore({ | |
todos: { | |
items: ['Install easy-peasy', 'Define your model', 'Have fun'], | |
// 👇 define actions directly on your model | |
add: action((state, payload) => { | |
// simply mutate state to update, and we convert to immutable updates | |
state.items.push(payload) | |
// (you can also return a new immutable version of state if you prefer) | |
}) | |
} | |
}); | |
const App = () => ( | |
// 👇 wrap your app to expose the store | |
<StoreProvider store={store}> | |
<TodoList /> | |
</StoreProvider> | |
) | |
function TodoList() { | |
// 👇 use hooks to get state or actions | |
const todos = useStore(state => state.todos.items) | |
const add = useActions(actions => actions.todos.add) | |
return ( | |
<div> | |
{todos.map((todo, idx) => <div key={idx}>{todo}</div>)} | |
<AddTodo onAdd={add} /> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment