Last active
November 7, 2018 22:41
-
-
Save Justkant/a3287b89682a215a8b8b67c50f690b11 to your computer and use it in GitHub Desktop.
redux like concept with react 16 context and react 16.7 beta hooks (https://codesandbox.io/s/jvklvmp8l9)
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 React from "react"; | |
import Provider from "./Provider"; | |
import Counter from "./Counter"; | |
const App = ({ reducer, initialState }) => { | |
return ( | |
<Provider reducer={reducer} initialState={initialState}> | |
<Counter /> | |
</Provider> | |
); | |
}; | |
export default App; |
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 { createContext } from "react"; | |
export const StoreContext = createContext({}); |
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 React, { useContext } from "react"; | |
import { StoreContext } from "./context"; | |
const Counter = () => { | |
const [state, dispatch] = useContext(StoreContext); | |
return ( | |
<> | |
Count: {state.count} | |
<button onClick={() => dispatch({ type: "reset" })}>Reset</button> | |
<button onClick={() => dispatch({ type: "increment" })}>+</button> | |
<button onClick={() => dispatch({ type: "decrement" })}>-</button> | |
</> | |
); | |
}; | |
export default Counter; |
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 React from "react"; | |
import { render } from "react-dom"; | |
import App from "./App"; | |
const initialState = { count: 0 }; | |
function reducer(state, action) { | |
switch (action.type) { | |
case "reset": | |
return initialState; | |
case "increment": | |
return { count: state.count + 1 }; | |
case "decrement": | |
return { count: state.count - 1 }; | |
default: | |
return state; | |
} | |
} | |
render( | |
<App reducer={reducer} initialState={initialState} />, | |
document.getElementById("root") | |
); |
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 React, { useReducer } from "react"; | |
import { StoreContext } from "./context"; | |
const Provider = ({ reducer, initialState, children }) => { | |
const store = useReducer(reducer, initialState); | |
return ( | |
<StoreContext.Provider value={store}> | |
{children} | |
</StoreContext.Provider> | |
); | |
}; | |
export default Provider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Components and logic from https://reactjs.org/docs/hooks-reference.html#usereducer