Improve state management [SYNTH-263]#1433
Conversation
7da54be to
5134e57
Compare
870aaf6 to
bea3dff
Compare
| const [pref, setPref] = useState(PreferencesSystem.getUserPreference(preference)) | ||
| useEffect(() => { | ||
| PreferencesSystem.setUserPreference(preference, pref) | ||
| }, [pref]) |
There was a problem hiding this comment.
This will fire a preferenceChanged event for every preference on mount and twice in dev strictmode
instead, I would suggest
<Checkbox {...props} label={label} checked={pref} onClick={
setPref(val)
PreferencesSystem.setUserPreferences(preference, val)
} />There was a problem hiding this comment.
This will fire a preferenceChanged event for every preference on mount and twice in dev strictmode
why is this an issue?
instead, I would suggest
<Checkbox {...props} label={label} checked={pref} onClick={ setPref(val) PreferencesSystem.setUserPreferences(preference, val) } />
this is a very strong react anti-pattern. If setPref gets called somewhere else, after this change, it will lead to the UI being in an invalid state.
There was a problem hiding this comment.
I agree with @0xda157; there's not much of a cost to the event being fired repeatedly since we have relatively few listeners and preferences, and it avoids ending up with an invalid UI state
There was a problem hiding this comment.
So we are reading from the preferences and setting the preferences...
There are 13 preference events being triggered and it seems that 8 do not have listeners:
ReportAnalytics, SubsystemGravity, RenderSceneTag, RenderScoreboard, ShowViewCube
ReportAnalytics & SubsystemGravity might be problematic:
-> ReportAnalytics calls sendMetaData() [eventually] -> sending to google analytics
-> calls GetMotorSettings() and rewrites 2 torque limits on a constraint
8f8052e to
68c28c7
Compare
Task
SYNTH-263
Symptom
many of our components include
useReducers purely to avoid manage state in the correct react waySolution
this pr improve the state handling in many locations, primarily by copying the state using their
setfunction instead of directly mutating them.Note
There are few more places with very poor state management, but I've deemed them too difficult to improve in this pr. Ideally they will be addressed later.
Verification
Playwright tests pass, and the UI seems to work as expected.
Before merging, ensure the following criteria are met: