Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

replace useState hack by a helper utility that is using ref and explaining what's happening

Oleg Isonen5c01b4baa1d4
diff --git a/apps/designer/app/canvas/shared/breakpoints.ts b/apps/designer/app/canvas/shared/breakpoints.ts
index 401fa02d83..54f5b92052 100644
--- a/apps/designer/app/canvas/shared/breakpoints.ts
+++ b/apps/designer/app/canvas/shared/breakpoints.ts
@@ -1,4 +1,4 @@
-import { useEffect, useState } from "react";
+import { useEffect } from "react";
import store from "immerhin";
import type { Breakpoint } from "@webstudio-is/css-data";
import { useSubscribe } from "~/shared/pubsub";
@@ -10,10 +10,11 @@ import {
} from "~/shared/nano-states";
import { publish } from "~/shared/pubsub";
import { addMediaRules } from "./styles";
+import { useSyncInitializeOnce } from "~/shared/hook-utils";
export const useInitializeBreakpoints = (breakpoints: Array<Breakpoint>) => {
const [, setCurrentBreakpoints] = useBreakpoints();
- useState(() => {
+ useSyncInitializeOnce(() => {
setCurrentBreakpoints(breakpoints);
addMediaRules(breakpoints);
});
diff --git a/apps/designer/app/canvas/shared/design-tokens.ts b/apps/designer/app/canvas/shared/design-tokens.ts
index 0f5cd50bff..d70eaf7f3b 100644
--- a/apps/designer/app/canvas/shared/design-tokens.ts
+++ b/apps/designer/app/canvas/shared/design-tokens.ts
@@ -1,12 +1,14 @@
import type { DesignToken } from "@webstudio-is/design-tokens";
-import { useEffect, useState } from "react";
+import { useEffect } from "react";
+import { useSyncInitializeOnce } from "~/shared/hook-utils";
import { useDesignTokens } from "~/shared/nano-states";
import { publish } from "~/shared/pubsub";
export const useInitializeDesignTokens = (designTokens: Array<DesignToken>) => {
const [, setDesignTokens] = useDesignTokens();
- // Trick to set it only once.
- useState(() => setDesignTokens(designTokens));
+ useSyncInitializeOnce(() => {
+ setDesignTokens(designTokens);
+ });
};
export const usePublishDesignTokens = () => {
diff --git a/apps/designer/app/shared/hook-utils/index.ts b/apps/designer/app/shared/hook-utils/index.ts
new file mode 100644
index 0000000000..8241cea1ae
--- /dev/null
+++ b/apps/designer/app/shared/hook-utils/index.ts
@@ -0,0 +1 @@
+export * from "./use-sync-initialize-once";
diff --git a/apps/designer/app/shared/hook-utils/use-sync-initialize-once.ts b/apps/designer/app/shared/hook-utils/use-sync-initialize-once.ts
new file mode 100644
index 0000000000..0c49261657
--- /dev/null
+++ b/apps/designer/app/shared/hook-utils/use-sync-initialize-once.ts
@@ -0,0 +1,14 @@
+import { useRef } from "react";
+
+// This hook allows to
+// 1. run a function only once on the client
+// 2. run it server-side
+// 3. run the function synchronously in both
+// Can't use effect for that (async).
+// Can't use useState initializer because it can be called twice in strict mode and also isn't designed for this.
+export const useSyncInitializeOnce = (fn: () => void) => {
+ const ref = useRef(false);
+ if (ref.current) return;
+ ref.current = true;
+ fn();
+};
diff --git a/packages/design-tokens/README.md b/packages/design-tokens/README.md
index 867393882b..93b92f3b6f 100644
--- a/packages/design-tokens/README.md
+++ b/packages/design-tokens/README.md
@@ -1,3 +1,3 @@
-# Webstudio Project
+# Webstudio Design Tokens
-Project related functionalities.
+Shared functionality for Design Tokens
diff --git a/packages/project/README.md b/packages/project/README.md
index 867393882b..1b2038b7b0 100644
--- a/packages/project/README.md
+++ b/packages/project/README.md
@@ -1,3 +1,3 @@
# Webstudio Project
-Project related functionalities.
+Project related functionalities. This was temporarily placed here to reuse between other packages, but we need to split this into separate packages and probably remove this package entirely or keep it only for "project" specific functionality.