Last active
December 10, 2021 08:54
-
-
Save shotaK/856467197a41b54287c751e31db14841 to your computer and use it in GitHub Desktop.
Typescript React Context
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
// 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