Last active
November 5, 2018 20:05
-
-
Save ctrlplusb/59a0f1573777bebed87ce375aa7d69ac to your computer and use it in GitHub Desktop.
Easy Peasy Primary API
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 { StoreProvider, createStore, useStore, useAction } from 'easy-peasy'; | |
// π firstly, create your store by providing your model | |
const store = createStore({ | |
todos: { | |
items: ['Install easy-peasy', 'Build app', 'Profit'], | |
// π define actions | |
add: (state, payload) => { | |
state.items.push(payload) // π you mutate state to update (we convert | |
// to immutable updates) | |
} | |
} | |
}); | |
function App() { | |
return ( | |
// π secondly, surround your app with the provider to expose the store to your app | |
<StoreProvider store={store}> | |
<TodoList /> | |
</StoreProvider> | |
); | |
} | |
function TodoList() { | |
// π finally, use hooks to get state or actions. your component will receive | |
// updated state automatically | |
const todos = useStore(state => state.todos.items) | |
const add = useAction(dispatch => dispatch.todos.add) | |
return ( | |
<div> | |
{todos.map((todo, idx) => <div key={idx}>{todo.text}</div>)} | |
<AddTodo onAdd={add} /> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment