Skip to main content

<State>

Retrieve state from a store and render content based on it. Render again when that state changes.

Properties

All properties are optional.

PropertyDescription
render or childrenThe render function. This function will be called with the state as it's first parameter.
fromWhat store to retrieve state from.
pathThe state path to retrieve. Specified as a dotted path string, with support for arrays. Sample: "my.field.path[10].name"
selectA function that selects the required state.
constantWhen true, the State component will query state and render only once.
strictWhen true, the State component will track accessed keys on every update instead of on just the first one.
defaultThe default value when the state is undefined.
throttleMilliseconds to throttle change requests
debounceMilliseconds to debounce change requests
debugWhen true, log messages regarding state changes will be printed to the console.
verboseWhen true, verbose log messages are printed to the console.
idDebug log uses this to identify components
onMountCalled with the current state when the component mounts.
onUnmountCalled with the current state when the component unmounts.

Accessing

Accessing state is done using the State component:

register({
state: ({ set }) => ({
counter: 0,
inc: () =>
set(state => {
state.counter++;
}),
dec: () =>
set(state => {
state.counter--;
})
})
});

const App = () => (
<State>
{state => (
<>
<div>Counter: {state.counter}</div>
<button onClick={state.inc}>+</button>
<button onClick={state.dec}>-</button>
</>
)}
</State>
);

Debugging

Using debug and verbose can help find problems:

register({
state: ({ set }) => ({
counter: 0,
inc: () =>
set(state => {
state.counter++;
}),
dec: () =>
set(state => {
state.counter--;
})
})
});

const App = () => (
<State debug verbose>
{state => (
<>
<div>Counter: {state.counter}</div>
<button onClick={state.inc}>+</button>
<button onClick={state.dec}>-</button>
</>
)}
</State>
);

Enabling debug and verbose on all State components:

register({
state: ({ set }) => ({
counter: 0,
inc: () =>
set(state => {
state.counter++;
}),
dec: () =>
set(state => {
state.counter--;
})
})
});

State.Debug = true;
State.Verbose = true;

const App = () => (
<State>
{state => (
<>
<div>Counter: {state.counter}</div>
<button onClick={state.inc}>+</button>
<button onClick={state.dec}>-</button>
</>
)}
</State>
);