@blocklet/pages-kit 0.6.103 → 0.6.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ import { joinURL } from 'ufo';
10
10
  import { create } from 'zustand';
11
11
  import { immer } from 'zustand/middleware/immer';
12
12
  import { preloadComponents } from '../../api';
13
+ import { useDeepCompareMemo } from '../../hooks/use-deep-compare-memo';
13
14
  import { PreloadComponentScriptModule } from '../../types';
14
15
  import { PreloadComponentsStateGlobalVariableName } from '../../types/preload';
15
16
  import { mergeComponent, parsePropertyValue } from '../../utils/property';
@@ -252,7 +253,7 @@ export function useComponent({ instanceId, componentId, properties, locale, dev
252
253
  // mode: production 不会有 transpile.props, dev 模式下会有 transpile.props,优先级最低
253
254
  const componentProps = transpile?.props ?? preload?.props;
254
255
  // 对 properties 进行转译,优先级中等
255
- const dynamicComponentProps = useMemo(() => {
256
+ const dynamicComponentProps = useDeepCompareMemo(() => {
256
257
  return Object.fromEntries(Object.entries(properties ?? {})
257
258
  .map(([key, { value }]) => {
258
259
  const property = componentProperties?.[key]?.data;
@@ -269,7 +270,7 @@ export function useComponent({ instanceId, componentId, properties, locale, dev
269
270
  return [propKey, v];
270
271
  })
271
272
  .filter((i) => !!i));
272
- }, [JSON.stringify(properties), JSON.stringify(componentProperties), locale, dev?.defaultLocale]);
273
+ }, [properties, componentProperties, locale, dev?.defaultLocale]);
273
274
  return {
274
275
  Component: transpile?.Component ?? preload?.Component,
275
276
  EditComponent: transpile?.EditComponent,
@@ -0,0 +1,4 @@
1
+ import { useDeepCompareMemo } from './use-deep-compare-memo';
2
+ export const useDeepCompareCallback = (callback, deps) => {
3
+ return useDeepCompareMemo(() => callback, deps);
4
+ };
@@ -0,0 +1,9 @@
1
+ import { useRef } from 'react';
2
+ import isEqual from 'react-fast-compare';
3
+ export const useDeepCompareMemo = (factory, deps) => {
4
+ const ref = useRef(null);
5
+ if (!ref.current || !isEqual(ref.current.deps, deps)) {
6
+ ref.current = { deps, value: factory() };
7
+ }
8
+ return ref.current.value;
9
+ };