Context API example using TypeScript and custom Provider/Consumer
Check src/contexts/AppContext.tsx for the definition and App.tsx for usage.
There are two examples of consumers in this project:
- src/components/Control.tsx for usage with the useContext hook:
const app = useContext(AppContext); - src/components/Timeline.tsx for usage with the custom useAppContext hook.
This is a didactic example, but ideally, you should not export AppContext in src/contexts/AppContext.tsx, but only the providr AppContextProvider and the consumer useAppContext. This way, external users will not have unnecessary imports to choose.
[outdated but can be useful] PT-BR Lesson: https://youtu.be/niTUo4u1xlY