Last active
September 5, 2023 07:38
-
-
Save imkarimkarim/594fc6a155b9abe9c5f93f7835ab5fb2 to your computer and use it in GitHub Desktop.
React Context in Typescript
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, FC, useContext, useState } from 'react'; | |
const Context = createContext<any>(null); | |
type Props = { | |
children: JSX.Element[] | JSX.Element | string; | |
}; | |
const StateProvider: FC<Props> = ({ children }) => { | |
const [state, setState] = useState(); | |
return ( | |
<Context.Provider value={{ state, setState }}>{children}</Context.Provider> | |
); | |
}; | |
export const useTheState = () => { | |
return useContext(Context); | |
}; | |
export default StateProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment