Last active
November 19, 2020 23:42
-
-
Save fmontone/d97539aa318ffe55d3d791edef8db312 to your computer and use it in GitHub Desktop.
React Context + Hook (PropTypes)
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, useContext } from 'react'; | |
import PropTypes from 'prop-types'; | |
const ExampleContext = createContext(); | |
function ExampleProvider({ children }) { | |
return ( | |
<ExampleContext.Provider value={}> | |
{children} | |
</ExampleContext.Provider> | |
) | |
} | |
function useExample() { | |
const context = useContext(ExampleContext); | |
if(!context) { | |
throw new Error('useExample must be used insede ExampleProvider') | |
} | |
} | |
ShowcaseProvider.propTypes = { | |
children: PropTypes.oneOfType([ | |
PropTypes.func, | |
PropTypes.string, | |
PropTypes.node, | |
]).isRequired, | |
}; | |
export {ExampleProvider, useExample} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment