useSelector<State, Output> function

Output? useSelector<State, Output>(
  1. Selector<State, Output> selector, [
  2. EqualityFn? equalityFn
])

A hook to access the redux store's state. This hook takes a selector function as an argument. The selector is called with the store state.

This hook takes an optional equality comparison function as the second parameter that allows you to customize the way the selected state is compared to determine whether the widget needs to be re-built. The default equality comparison function is one that uses referential equality.

Example

class StoreUser extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final someCount = useSelector<AppState, int>(selectSomeCount);
    return YourWidgetHierarchy(someCount: someCount);
  }
}

Implementation

Output? useSelector<State, Output>(Selector<State, Output> selector,
    [EqualityFn? equalityFn]) {
  final store = useStore<State>();
  final snap = useStream<Output>(
      store.onChange.map(selector).distinct(equalityFn),
      initialData: selector(store.state));
  return snap.data;
}