Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-chairs-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mod-protocol/react": patch
---

fix: debounce onload event calling to prevent infinite calling (#135)
2 changes: 2 additions & 0 deletions packages/core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ export class Renderer {

if (action.ref) {
set(this.refs, action.ref, { response, progress: 100 });
this.onTreeChange();
}

if (action.onsuccess) {
Expand All @@ -651,6 +652,7 @@ export class Renderer {

if (action.ref) {
set(this.refs, action.ref, { error });
this.onTreeChange();
}

this.asyncAction = null;
Expand Down
12 changes: 10 additions & 2 deletions packages/react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ const WrappedHorizontalLayoutRenderer = <T extends React.ReactNode>(props: {
element: Extract<ModElementRef<T>, { type: "horizontal-layout" }>;
}) => {
const { component: Component, element } = props;
const { events, type, elements, ...rest } = element;
const { type, elements, ...rest } = element;

// Prevents the onLoad event from being called multiple times when
// the tree changes and reference to the events object changes.
// Assumes events are immutable over the lifecycle of a component
const [events] = React.useState(element.events);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a comment would be helpful. otherwise lgtm if events are immutable over the lifecycle of a component, which I think they are


React.useEffect(() => {
events.onLoad();
Expand All @@ -189,7 +194,10 @@ const WrappedVerticalLayoutRenderer = <T extends React.ReactNode>(props: {
element: Extract<ModElementRef<T>, { type: "vertical-layout" }>;
}) => {
const { component: Component, element } = props;
const { events, type, elements, ...rest } = element;
const { type, elements, ...rest } = element;

// See WrappedHorizontalLayoutRenderer for explanation
const [events] = React.useState(element.events);

React.useEffect(() => {
events.onLoad();
Expand Down