Function: observer

react.observer

observer<P>(Component): React.FC<P>

Higher-Order Component (HOC) that transforms a React component into a fine-grained reactive observer.

Type parameters

Name Type Description
P extends object The props type accepted by the wrapped component.

Parameters

Name Type Description
Component ComponentType<P> The standard React component to wrap.

Returns

React.FC<P>

A reactive observer React component.

Remarks

Any reactive signal or proxy property read during the render lifecycle of the wrapped component will be automatically tracked as a dependency. When any of those dependencies mutate, the component re-renders.

Example

import React from 'react';
import { observer } from '@banksia/signals/react';
import { cartStore } from './cart-store';

export const CartCount = observer(() => {
  return <span>Items: {cartStore.items.length}</span>;
});