Skip to content

Instantly share code, notes, and snippets.

@imkarimkarim
Last active September 5, 2023 07:38
Show Gist options
  • Save imkarimkarim/594fc6a155b9abe9c5f93f7835ab5fb2 to your computer and use it in GitHub Desktop.
Save imkarimkarim/594fc6a155b9abe9c5f93f7835ab5fb2 to your computer and use it in GitHub Desktop.
React Context in Typescript
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