useEnvironment

@Environment(Model.self): reads the Observable an ancestor supplied with .environment(model). The component re-renders only when a property it read changes. With { optional: true }, like Model?.self, a missing object gives null instead of throwing.

Declaration

React
function useEnvironment<T extends Observable>(
  type: ObservableType<T>
): Bindable<T>

function useEnvironment<T extends Observable>(
  type: ObservableType<T>,
  options: {
    optional: true;
  }
): Bindable<T> | null

Parameters

  • type*ObservableType<T>
  • options.optional*true

Discussion

@example const library = useEnvironment(Library); return {library.count};

See also

More in Hooks.