Created
April 3, 2019 08:04
-
-
Save hastebrot/67d61c0af2add759e47e213ed6b06fe7 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 React from "react" | |
| import createStore from "storeon" | |
| import useStoreon from "storeon/react" | |
| import StoreContext from "storeon/react/context" | |
| import produce from "immer" | |
| import { Pane, Button } from "fannypack" | |
| export default () => { | |
| return ( | |
| <StoreContext.Provider value={store}> | |
| <Count /> | |
| </StoreContext.Provider> | |
| ) | |
| } | |
| export const increment = store => { | |
| store.on("@init", () => { | |
| return { count: 0 } | |
| }) | |
| store.on( | |
| "increment", | |
| produce(state => { | |
| state.count += 1 | |
| }) | |
| ) | |
| } | |
| export const store = createStore([ | |
| increment, | |
| process.env.NODE_ENV !== "production" && require("storeon/devtools"), | |
| process.env.NODE_ENV !== "production" && require("storeon/devtools/logger"), | |
| ]) | |
| export const Count = () => { | |
| const { dispatch, count } = useStoreon("count") | |
| return ( | |
| <Pane margin="10px" padding="10px" border> | |
| <Button onClick={() => dispatch("increment")}>count = {count}</Button> | |
| </Pane> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment