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
const StateContent = createContext(); | |
export function useStore () { | |
const { state, dispatch } = useContext(StateContext); | |
return [state, dispatch]; | |
} |
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
export function StateProvider ({ state: initialState, reducers, middleware, children }) { | |
const [state, dispatch] = useReducer((state, action) => { | |
return reducers.reduce((state, reducer) => reducer(state, action) || state, state); | |
}, initialState); | |
return ( | |
<StateContext.Provider value={{ state, dispatch }}> | |
{children} | |
</StateContext.Provider> | |
); |
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, { useState } from 'react'; | |
function Counter () { | |
const [count, setCount] = useState(0); | |
return ( | |
<> | |
<span> | |
Count: {count} | |
</span> | |
<button onClick={() => setCount(count + 1)}> |
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
export class StateProvider extends React.PureComponent { | |
static defaultProps = { | |
state: {}, | |
reducers: [], | |
middleware: [] | |
}; | |
state = this.props.state; | |
_dispatch = action => { |
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
export default function Root () { | |
return ( | |
<StateProvider | |
state={initialState} | |
reducers={[countReducer]} | |
middleware={[countMiddleware]} | |
> | |
<MyApp /> | |
</StateProvider> | |
); |
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
function countMiddleware ({ type, payload }, { count }) { | |
if (type === ADD_N) { | |
console.log(`${payload} + ${count} = ${payload + count}`); | |
return null; | |
} | |
} |
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
export default function SomeCount () { | |
return ( | |
<StateConsumer> | |
{({ state, dispatch }) => ( | |
<> | |
<p> | |
Count: {state.count} | |
</p> | |
<button onClick={() => dispatch(addOne())}> | |
+ 1 |
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
export default function Root () { | |
return ( | |
<StateProvider state={initialState} reducers={[countReducer]}> | |
<MyApp /> | |
</StateProvider> | |
); | |
} |
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
function countReducer ({ count, ...state }, { type, payload }) { | |
switch (type) { | |
case ADD_N: | |
return { ...state, count: count + payload }; | |
case ADD_ONE: | |
return { ...state, count: count + 1 }; | |
} | |
} |
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
export class Provider extends React.PureComponent { | |
static defaultProps = { | |
state: {}, | |
reducers: [] | |
}; | |
state = this.props.state; | |
_dispatch = action => { | |
const { reducers } = this.props; |