Skip to content

Choose a selector

Use useNativeState when a component needs to read and write a value. Use useNativeSelector when it only needs to read.

Both hooks accept paths. useNativeSelector also accepts a function:

const name = useNativeSelector('state.user.name');
const isSignedIn = useNativeSelector((state) => Boolean(state.user));

A path selector subscribes to a specific path. When a store update happens, notifications are sent to listeners for that path and related parent or child paths. React then compares the selected snapshot to its previous value.

Prefer paths for straightforward reads:

const itemCount = useNativeSelector('state.cart.itemCount');

A function selector is notified for every store update and runs again to produce its snapshot. React can skip rendering when the selected value is unchanged by Object.is.

const displayName = useNativeSelector((state) => state.user.name);

Prefer a primitive or an existing stable reference as the result. A selector that creates a new object or array on every call returns a new snapshot each time:

// Avoid: this makes a fresh object whenever the selector runs.
const profile = useNativeSelector((state) => ({ name: state.user.name }));

Select the values individually or keep derived object creation outside the selector when appropriate.

The store update implementation shallow-copies the root object but mutates the object or array at a nested path. As a result, a nested write such as setName('George') changes state.user.name but keeps the state.user object reference.

If a component selects state.user, React may see the same object snapshot and skip rendering after a nested property changes. Select the leaf path (state.user.name) when that is what the component displays. If a parent-object subscriber must observe a change, replace the parent with a new object rather than mutating one of its children.

This distinction is important: path notifications do not guarantee a render when the selected snapshot remains the same reference.