Files
webstudio/packages/react-sdk/src/variable-state.tsx
T
Ivan Starkov 13da170ef4 experimental: Allow json in template literals (#3886)
## Description
closes #3872


- [x] - Builder support
- [x] - Publish support

## Steps for reproduction

Example

https://add-json.staging.webstudio.is/builder/8160e16d-5ab4-4249-86de-0c98a64526a3

https://test-json-ki9yj.wstd.work/

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-08-11 11:50:18 +03:00

16 lines
469 B
TypeScript

import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
import { useState, useMemo, type Dispatch, type SetStateAction } from "react";
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];
};