Skip to content

Instantly share code, notes, and snippets.

@shotaK
Last active December 10, 2021 08:54
Show Gist options
  • Save shotaK/856467197a41b54287c751e31db14841 to your computer and use it in GitHub Desktop.
Save shotaK/856467197a41b54287c751e31db14841 to your computer and use it in GitHub Desktop.
Typescript React Context
// Create context
import { createContext } from 'react';
export type ExampleContext = {
someFunction?: (arg) => void;
someProperty?: string[];
};
export default createContext<ExampleContext>(null);
// Get properties
const { someFunction, someProperty } = useContext(ExampleContext);
// Providing context
<ExampleContext.Provider value={{ someFunction: (arg) => {}, someProperty: [] }}
>
</ExampleContext.Provider>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment