Created
February 8, 2019 08:01
-
-
Save ctrlplusb/42ae58df0952c106732ec9f4fb24ca6f 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 { StoreProvider, createStore, useStore, useActions } from 'easy-peasy'; | |
// π create your store, providing the model | |
const store = createStore({ | |
todos: { | |
items: ['Install easy-peasy', 'Build app', 'Profit'], | |
// π define actions directly on your model | |
add: (state, payload) => { | |
// do simple mutation to update state, and we make it an immutable update | |
state.items.push(payload) | |
// (you can also return a new immutable instance 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