fix(react): debounce onload events - #137
Conversation
🦋 Changeset detectedLatest commit: 4c8e600 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
d910a83 to
cbd9624
Compare
| events.onLoad(); | ||
| }, [events]); | ||
| if (!loaded) { | ||
| events.onLoad(); |
There was a problem hiding this comment.
isn't the real issue here that this useEffect is being triggered again when it shouldn't? the only way for that to happen is if the events object is different - and it seems its different unnecessarily? perhaps we're not memoizing it before passing it to this component when we should?
There was a problem hiding this comment.
I wonder if it's coming from reconstructing the anonymous function here when mapCurrentTree is called
mod/packages/core/src/renderer.ts
Lines 1197 to 1203 in 3b751db
There was a problem hiding this comment.
not sure - I think perhaps our core renderer doesn't follow react's component model with uniquely deduplicated components (keys) vs just remounting all the components. Could be worth passing a key or trying to debug whether thats what the cause is.
There was a problem hiding this comment.
did a bit of reading and useEffect compares reference types in the dependency array based on their location in memory. so what i think is happening is once the tree rerenders when the context changes, it creates a new anonymous function which will have a new location in memory and hence retriggers the useEffect hook
when i change the latch to a simple state variable that takes on the initial value of the events prop, it doesn't do the infinite looping anymore:
const [events] = React.useState(element.events);
React.useEffect(() => {
events.onLoad();
}, [events]);cbd9624 to
3b751db
Compare
3b751db to
96322e7
Compare
96322e7 to
892e382
Compare
| const { events, type, elements, ...rest } = element; | ||
| const { type, elements, ...rest } = element; | ||
|
|
||
| const [events] = React.useState(element.events); |
There was a problem hiding this comment.
a comment would be helpful. otherwise lgtm if events are immutable over the lifecycle of a component, which I think they are
892e382 to
4c8e600
Compare
Change Summary
Fixes #135 by introducing a state variable which debounces the call to
onloadeventsMerge Checklist