Last active
October 15, 2019 16:14
-
-
Save udittyagi/b741137ce802f25e373bea72d2533bdc to your computer and use it in GitHub Desktop.
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, { | |
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