Files
webstudio/packages/react-sdk/src/variable-state.tsx
T
Bogdan Chadkin f75a837c9e refactor: add sdk runtime (#4417)
Similar to react-sdk/runtime added another entry point to sdk package
which includes all the stuff imported on published sites to reduce js
size.

./fixtures/webstudio-remix-netlify-functions/build/client: 392kB
(-176kB)
2024-11-13 17:20:42 +04:00

19 lines
482 B
TypeScript

import { useState, useMemo, type Dispatch, type SetStateAction } from "react";
import {
createJsonStringifyProxy,
isPlainObject,
} from "@webstudio-is/sdk/runtime";
export const useVariableState = <S,>(
initialState: S | (() => S)
): [S, Dispatch<SetStateAction<S>>] => {
const [state, setState] = useState(initialState);
const value = useMemo(
() => (isPlainObject(state) ? createJsonStringifyProxy(state) : state),
[state]
);
return [value, setState];
};