useDvaConnect function

DvaConnect useDvaConnect({
  1. String? namespace,
  2. List<String>? listenKeys,
  3. MapState? mapState,
  4. ShouldUpdate? shouldUpdate,
  5. VoidCallback? didMounted,
})

A hook that connects a widget to the Dva Store.

Automatically subscribes to Store state changes and triggers widget rebuild when the specified state changes. Supports the following features:

  • Namespace filtering via namespace
  • Key-level filtering via listenKeys
  • Custom state mapping via mapState
  • Custom update strategies via shouldUpdate
  • Mount callback via didMounted

Returns a DvaConnect object containing props, state, and store.

Example usage:

final connect = useDvaConnect(
  namespace: 'counter',
  listenKeys: ['count'],
  mapState: (rootState) => {'counter': rootState['counter']},
  shouldUpdate: (lState, nState) => lState.updateAt != nState.updateAt,
  didMounted: () => print('Component mounted'),
);

connect.props?.dispatch(Type('increment'));
connect.state?['counter'];
connect.store?.rootState;

Implementation

DvaConnect useDvaConnect({
  /// Optional namespace to listen to. If provided, only state changes
  /// under this namespace will trigger widget rebuilds.
  String? namespace,

  /// Optional list of keys to listen to under the specified namespace.
  /// If provided, only changes to these specific keys will trigger rebuilds.
  List<String>? listenKeys,

  /// Optional custom state mapping function. Transforms the root state
  /// into a custom shape for the widget to consume.
  MapState? mapState,

  /// Optional custom update decision function. Determines whether a
  /// state change should trigger a widget rebuild.
  ShouldUpdate? shouldUpdate,

  /// Optional callback that fires after the component is first mounted
  /// and the initial state has been initialized.
  VoidCallback? didMounted,
}) {
  return use(
    _DvaConnectHook(
      namespace: namespace,
      listenKeys: listenKeys,
      mapState: mapState,
      shouldUpdate: shouldUpdate,
      didMounted: didMounted,
    ),
  );
}