Skip to content

Instantly share code, notes, and snippets.

@udittyagi
Last active October 15, 2019 16:14
Show Gist options
  • Save udittyagi/b741137ce802f25e373bea72d2533bdc to your computer and use it in GitHub Desktop.
Save udittyagi/b741137ce802f25e373bea72d2533bdc to your computer and use it in GitHub Desktop.
import React, {
useState,
createContext,
useContext} from 'react';
const defaultValue = {}
const BreakpointContext = createContext(defaultValue);
const BreakpointProvider = ({children, queries}) => {
//State in which we maintain our matching query
const [queryMatch, setQueryMatch] = useState({});
//Our Logic to get matching query goes here
return (
<BreakpointContext.Provider value={queryMatch}>
{children}
</BreakpointContext.Provider>
)
}
function useBreakpoint() {
const context = useContext(BreakpointContext);
if(context === defaultValue) {
throw new Error('useBreakpoint must be used within BreakpointProvider');
}
return context;
}
export {useBreakpoint, BreakpointProvider};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment