useContext<T> function
Returns the value of the nearest Context.Provider for the provided context
object every time that context is
updated.
The usage is similar to that of a Context.Consumer in that the return type of useContext is dependent upon the typing of the value passed into createContext and Context.Provider.
Note: there are two rules for using Hooks:
- Only call Hooks at the top level.
- Only call Hooks from inside a DartFunctionComponent.
Example:
Context countContext = createContext(0);
UseCallbackTestComponent(Map props) {
final count = useContext(countContext);
return react.div({}, [
react.div({}, ['The count from context is $count']), // initially renders: 'The count from context is 0'
]);
}
Learn more: reactjs.org/docs/hooks-reference.html#usecontext.
Implementation
T useContext<T>(Context<T> context) => ContextHelpers.unjsifyNewContext(React.useContext(context.jsThis)) as T;