Skip to content

Instantly share code, notes, and snippets.

@fmontone
Last active November 19, 2020 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmontone/d97539aa318ffe55d3d791edef8db312 to your computer and use it in GitHub Desktop.
Save fmontone/d97539aa318ffe55d3d791edef8db312 to your computer and use it in GitHub Desktop.
React Context + Hook (PropTypes)
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