useMemo<T, V> function

T useMemo<T, V>(
  1. T create(), [
  2. List<V>? dependencies
])

Returns a memoized value.

Pass a create function and an array of dependencies. useMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.

Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useLayoutEffect, not useMemo.

If no dependencies provided, a new value will be computed on every render.

Implementation

T useMemo<T, V>(
  T Function() create, [
  List<V>? dependencies,
]) {
  var useMemoHook = useHook();
  useMemoHook ??= setupHook(_UseMemoHook<T, V>());

  if (useMemoHook is! _UseMemoHook) {
    throw Exception(
      'Expecting hook of type: $_UseMemoHook '
      'but got: ${useMemoHook.runtimeType}. '
      'Please make sure your hooks call order is not dynamic.',
    );
  }

  useMemoHook
    ..updateDependencies(dependencies)
    ..updateComputation(create);

  return useMemoHook.computationResult;
}