v1.0.0-preview.10, LiveState revamp & bug fixes
Pre-release
Pre-release
·
66 commits
to main
since this release
We have revamped LiveState
and useLiveState
in this version due to developer feedback, and to accomplish this we had to make a breaking change. Rather than have separate state
and data
values in LiveState
, there is now a single value.
If you are currently using LiveState
in your application, here are the relevant changes:
// replace T with your type if in TypeScript, eg <string>
const yourLiveState = container.initialObjects.liveState as LiveState<T>;
// register listeners, now (state: T, local: boolean) instead of (state, value, local)
yourLiveState.on("stateChanged", (state, local) => {
// handle change
});
// initialize LiveState with initial state (this previously was optional and didn't work)
// default value can be any JSON serializable value, such as string, number, object, etc.
await yourLiveState.initialize("your-default-value");
// when making changes, use `.set(state: T)` instead of `.changeState(state: string, data?: T)`
yourLiveState.set("your-new-value");
For Live Share React, similar to useSharedState
, you can now simply do this:
// in TypeScript replace <T> with your type (eg string), otherwise delete <T>
const [liveState, setLiveState] = useLiveState<T>("key", "your-default-value");
return (
{liveState}
);
Other changes include:
- Fixed bug where
initialState
was not getting set when usingLiveState.initialize()
- Fixed bug where
localUserCursor
prop in Live Share React'suseLiveCanvas
hook was not properly setting changes