context.js
31 lines
| 1 | /** |
| 2 | * WordPress Dependencies |
| 3 | */ |
| 4 | import { createContext, useReducer } from '@wordpress/element'; |
| 5 | |
| 6 | /** |
| 7 | * Internal Dependencies |
| 8 | */ |
| 9 | import initialState from './initial'; |
| 10 | import reducers from './reducers'; |
| 11 | import actions from './actions'; |
| 12 | |
| 13 | // Create the context |
| 14 | export const OneClickContext = createContext(initialState()); |
| 15 | |
| 16 | export default function ({ children }) { |
| 17 | const [state, dispatch] = useReducer(reducers, { ...initialState() }); |
| 18 | |
| 19 | const store = { |
| 20 | ...state, |
| 21 | dispatch, |
| 22 | ...actions(state, dispatch), |
| 23 | }; |
| 24 | |
| 25 | return ( |
| 26 | <OneClickContext.Provider value={store}> |
| 27 | {children} |
| 28 | </OneClickContext.Provider> |
| 29 | ); |
| 30 | } |
| 31 |