Last active
March 23, 2019 20:59
-
-
Save mtshrmn/46da15ec26598f4cafab3bcd95a19809 to your computer and use it in GitHub Desktop.
Generating React contexts with ease!
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, {createContext, useReducer} from "react"; | |
const generateContext = ({initialState = {}, reducer}) => { | |
const Context = createContext(initialState); | |
const provideComponent = Component => { | |
const wrapper = props => { | |
const [store, dispatch] = useReducer(reducer, initialState); | |
return ( | |
<Context.Provider value={{store, dispatch}}> | |
<Component {...props}/> | |
</Context.Provider> | |
); | |
}; | |
return wrapper; | |
}; | |
return [Context, provideComponent]; | |
}; | |
export default generateContext; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment