useContext<T> function

T useContext<T>(
  1. Context<T> context
)

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 the first argument of uiFunction.

Example:

Context countContext = createContext(0);

UiFactory<UseContextExampleProps> UseContextExample = uiFunction(
  (props) {
    final count = useContext(countContext);

    return Dom.div()(
      Dom.div()(
        'The count from context is $count',
      ), // initially renders: 'The count from context is 0'
    );
  },
  _$UseContextExampleConfig, // ignore: undefined_identifier
);

Learn more: reactjs.org/docs/hooks-reference.html#usecontext.

Implementation

T useContext<T>(Context<T> context) => react_hooks.useContext(context.reactDartContext);