Answer the question
In order to leave comments, you need to log in
How to use context with useContext hook?
Hello.
App.tsx
const Hello = createContext<string>('Hello');
function App() {
return (
<Hello.Provider value='Hi'>
<MyComponent />
</Theme.Provider>
);
}
export default App;
const MyComponent: React.FC<> = () => {
const msg = useContext<string>(Hello);
return (
<span>{msg}</span>
)
}
Cannot find name 'Hello'.ts(2304)
Answer the question
In order to leave comments, you need to log in
rewrote the answer
here doc
const Hello = createContext<string>('Hello');
export default Hello;
import Hello from 'HelloContext';
import MyComponent from 'MyComponent';
function App() {
return (
<Hello.Provider value='Hi'>
<MyComponent />
</Hello.Provider>
);
}
export default App;
import HelloContext from 'HelloContext';
const MyComponent: React.FC<> = () => {
const msg = useContext(HelloContext );
return (
<span>{msg}</span>
)
}
export default MyComponent;
</Theme.Provider> <-- откуда Theme?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question