How can I implement @inject and observer in functional child components without explicitly importing the store with context and destructuring it. Repo and Deployed site below
Asked
Active
Viewed 778 times
1 Answers
0
You can make hook like that, so you won't need to import context every time, just this hook:
export const useStore = () => {
const store = React.useContext(storeContext)
if (!store) {
// this is especially useful in TypeScript so you don't need to be checking for null all the time
throw new Error('useStore must be used within a StoreProvider.')
}
return store
}
And you can still use inject
decorator with functional components if you prefer that way, it still works and it's totally fine way

Danila
- 15,606
- 2
- 35
- 67
-
Great! Thank you. – hildakh Jul 14 '20 at 18:25