This repository was archived by the owner on Jul 9, 2025. It is now read-only.
fix: misused 'useEffect' hook in Visual Editor (#1496) #1601
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
How does the redraw promblem happen
My observation was based on two scenarios: 1. change focus state by clicking a node 2. change node data by adding/deleting a node.
According to the profiler's result, every time we do those two operations, Visual Editor will be redrawn twice:
useEffecthook which syncs Shell props to local state(unexpected).So the redraw problem is basically caused by the misused
useEffecthook and unnecessary local states in Visual Editor.Why there are 'useEffect' hooks
At the very beginning, Visual Editor didn't sync states except 'obi json' with Shell, so all its states were local. As the evolution of Visual Editor, we are syncing more states between Visual and Shell, as a result, we hoisted many local states to shell layer. Thus
useEffecthook was used an observer to sync Shell props to Visual Editor's local states which will always trigger another Visual Editor rendering.Solution
As describe before, It is obviously an anti-pattern usage of the
useEffectaccording to the official doc, indicates our state management is not good enough. According to our discussion, the long term plan is, we migrate to theuseReducerhook to follow a redux-like state management pattern.In the short run, for fixing the redraw issue, the solution is to let Visual Editor be rendered only by props passed from Shell and remove some local states such as
focusedId.Previously, the state chain is:
now, the state chain is:
It's a quite straight forward fix after we hoisted visual editor states to Shell, but I think it would be much clearer by providing more context about it.
Task Item
Fixes #1496
Type of change
Please delete options that are not relevant.
Checklist
Screenshots
Please include screenshots or gifs if your PR include UX changes.