-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Fix/use editor state inside canvas" (#6105)
This reverts commit 4271d2e.
- Loading branch information
1 parent
e835574
commit 90af551
Showing
8 changed files
with
138 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
editor/src/components/canvas/ui-jsx-canvas-renderer/canvas-state-hooks.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react' | ||
import { shallowEqual } from '../../../core/shared/equality-utils' | ||
import type { EditorStorePatched } from '../../editor/store/editor-state' | ||
import type { StateSelector, Substores } from '../../editor/store/store-hook' | ||
import { | ||
CanvasStateContext, | ||
useEditorState, | ||
useRefEditorState, | ||
} from '../../editor/store/store-hook' | ||
import type { StoreKey } from '../../editor/store/store-hook-substore-types' | ||
import { ElementsToRerenderGLOBAL } from '../ui-jsx-canvas' | ||
|
||
export function useAllowRerenderOnlyOnAllElements() { | ||
return React.useCallback(() => ElementsToRerenderGLOBAL.current === 'rerender-all-elements', []) | ||
} | ||
|
||
export function useWrappedSelectorToPreventRerender<T, U>( | ||
selector: StateSelector<T, U>, | ||
allowRerender: () => boolean, | ||
): StateSelector<T, U> { | ||
const previousResultRef = React.useRef<U | null>(null) | ||
return React.useCallback( | ||
(state: T) => { | ||
if (allowRerender() || previousResultRef.current == null) { | ||
// if allowRerender() is false, we will not recompute the selected state, preventing a re-render of the owner component | ||
previousResultRef.current = selector(state) | ||
} | ||
return previousResultRef.current | ||
}, | ||
[selector, allowRerender], | ||
) | ||
} | ||
|
||
export const useCanvasState = <K extends StoreKey, S extends (typeof Substores)[K], U>( | ||
storeKey: S, | ||
allowRerender: () => boolean, | ||
selector: StateSelector<Parameters<S>[0], U>, | ||
selectorName: string, | ||
equalityFn: (oldSlice: U, newSlice: U) => boolean = shallowEqual, | ||
) => { | ||
return useEditorState( | ||
storeKey, | ||
useWrappedSelectorToPreventRerender(selector, allowRerender), | ||
selectorName, | ||
equalityFn, | ||
CanvasStateContext, | ||
) | ||
} | ||
|
||
/** | ||
* Like useCanvasState, but DOES NOT TRIGGER A RE-RENDER | ||
* | ||
* ONLY USE IT IF YOU ARE SURE ABOUT WHAT YOU ARE DOING | ||
*/ | ||
export const useRefCanvasState = <U,>( | ||
selector: StateSelector<EditorStorePatched, U>, | ||
explainMe = false, | ||
): { readonly current: U } => { | ||
return useRefEditorState(selector, explainMe, CanvasStateContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.