merge (boolean): Whether to merge the data with existing data (default: false)
Live State data persists indefinitely until manually removed - there is no automatic cleanup.
Copy
Ask AI
const liveStateDataId = 'uniqueId'; // A unique string identifier for the dataconst liveStateData = { 'key': 'value' }; // The data to be synced (can be any serializable type)const config = { merge: true };// Using HooksuseSetLiveStateData(liveStateDataId, liveStateData, config);// Using APIconst liveStateSyncElement = useLiveStateSyncUtils();liveStateSyncElement.setLiveStateData(liveStateDataId, liveStateData, config);
Copy
Ask AI
const liveStateDataId = 'uniqueId'; // A unique string identifier for the dataconst liveStateData = { 'key': 'value' }; // The data to be synced (can be any serializable type)const config = { merge: true };// Using HooksuseSetLiveStateData(liveStateDataId, liveStateData, config);// Using APIconst liveStateSyncElement = useLiveStateSyncUtils();liveStateSyncElement.setLiveStateData(liveStateDataId, liveStateData, config);
Copy
Ask AI
const liveStateDataId = 'uniqueId';const liveStateData = { 'key': 'value' }; // any type of dataconst config = { merge: true };const liveStateSyncElement = Velt.getLiveStateSyncElement();liveStateSyncElement.setLiveStateData(liveStateDataId, liveStateData, config);
The useLiveState() hook provides a familiar React’s useState() like API while automatically syncing state changes across all connected clients in real-time.
The hook accepts the following parameters:
id (string, required): Unique identifier for syncing this state across clients
initialValue (any, required): Initial state value
options (object, optional): Configuration object with the following properties:
syncDuration (number, optional): Debounce delay in milliseconds before syncing. Defaults to 50ms.
resetLiveState (boolean, optional): Whether to reset server state when the hook initializes. Defaults to false.
listenToNewChangesOnly (boolean, optional): When true, only receives changes that occur after subscribing and discards historical changes. Defaults to false.
Returns
[value, setValue, connectionState]
value: Current value of the data you set
setValue: Function to update the data
connectionState: Current server connection status
Copy
Ask AI
import { useLiveState } from "@veltdev/react";export function MyReactComponent() { const [counter, setCounter, serverConnectionStateInLiveState] = useLiveState<number> ("counter", 0, { syncDuration: 100, resetLiveState: true, listenToNewChangesOnly: true // receive only new changes from when the client subscribed }); useEffect(() => { console.log('serverConnectionStateInLiveState:', serverConnectionStateInLiveState); }, [serverConnectionStateInLiveState]); return ( <div> <button onClick={() => setCounter((counter || 0) - 1)}>-</button> <span>Counter: {counter}</span> <button onClick={() => setCounter((counter || 0) + 1)}>+</button> </div> );}
Copy
Ask AI
import { useLiveState } from "@veltdev/react";export function MyReactComponent() { const [counter, setCounter, serverConnectionStateInLiveState] = useLiveState<number> ("counter", 0, { syncDuration: 100, resetLiveState: true, listenToNewChangesOnly: true // receive only new changes from when the client subscribed }); useEffect(() => { console.log('serverConnectionStateInLiveState:', serverConnectionStateInLiveState); }, [serverConnectionStateInLiveState]); return ( <div> <button onClick={() => setCounter((counter || 0) - 1)}>-</button> <span>Counter: {counter}</span> <button onClick={() => setCounter((counter || 0) + 1)}>+</button> </div> );}