Unique key to identify the state.
Must be unique across the library. Consider using qualifiedName to avoid name collisions.
Default value in cases where no value exists for the given key or the value fails schema
validation.
This ensures that useServerState consistently returns a value of type TValue.
OptionalrefetchInterval?: numberOptional refresh interval for the value.
If set, will automatically perform a refresh every refreshInterval milliseconds to keep state
in sync with possible other clients looking at the same data. [in ms]
Scope in which the value should exist, either User, Global or Project.
Attempting to set value will fail when using the User scope and no user
is currently logged in. Check the canWrite option to prevent runtime error.
function TopicInfo() {
const [topic, setTopic, topicInfo] = useServerState(
qualifiedName(namespace, 'mostImportantTopic')
values.String(),
'Default Topic',
{ scope: 'Global' }
);
if (topicInfo.loading) return <div>Loading…</div>;
return (
<div>
<div>Most Important Topic: {topic}</div>
<button onClick={() => setTopic('Topic A')}>Set Topic A</button
<button onClick={() => setTopic('Topic B')}>Set Topic B</button
</div>
);
}
function UserCounter() {
const [value, setValue, { loading, canWrite }] = useServerState(
'value', values.Number(), 0,
{ scope: 'User', refetchInterval: 1000 }
);
return (
// Make sure to check `canWrite`: if no user is logged in, `setValue` will
// throw an exception.
<button disabled={!canWrite} onClick={() => setValue((value ?? 0) + 1)}>
Counter: {loading ? '–' : value}
</button>
);
}
Stores a JSON value on the server and returns a tuple of the current value and a setter function.
The value is validated against the provided schema. If for any reason the value in the server's key value store does not match that schema, the default value is returned instead.