Read and write state
useNativeState(path, initialValue?) returns a pair:
const [value, setValue] = useNativeState('state.profile.name', 'Mary');The first item is the current snapshot at the path. The second item is a setter for that path.
Set a value
Section titled “Set a value”Pass the value you want to store:
const [count, setCount] = useNativeState('state.counter', 0);
<button onClick={() => setCount(count + 1)}>Count: {count}</button>The setter accepts a value, not a React-style updater callback. Do not write setCount(current => current + 1); that function would be treated as the value. Derive a new value from the current snapshot instead.
For nested paths, the target’s parent object or array must exist. See state paths.
Initialize state
Section titled “Initialize state”For predictable startup state, provide the full state shape to <Root initial={...}> and omit the hook’s second argument:
<Root initial={{ counter: 0 }}> <App /></Root>const [count, setCount] = useNativeState('state.counter');About the optional second argument
Section titled “About the optional second argument”The hook accepts useNativeState(path, initialValue); it does not first check whether the path already has a value. It can therefore replace a value that was already initialized elsewhere. Prefer initializing through <Root> when you need deterministic shared state.
Share a slice between components
Section titled “Share a slice between components”Components that use the same path read from the same store:
function NameEditor() { const [name, setName] = useNativeState('state.user.name', 'Mary'); return <input value={name ?? ''} onChange={(event) => setName(event.target.value)} />;}
function NameGreeting() { const name = useNativeSelector('state.user.name'); return <p>Hello, {name}</p>;}Both components subscribe to the same value. When the editor writes a different name, the greeting observes the updated snapshot.