Skip to content

Fix editor lifecycle bugs in EmailEditor#480

Open
farhan523 wants to merge 2 commits into
unlayer:masterfrom
farhan523:fix/wrapper-lifecycle-bugs
Open

Fix editor lifecycle bugs in EmailEditor#480
farhan523 wants to merge 2 commits into
unlayer:masterfrom
farhan523:fix/wrapper-lifecycle-bugs

Conversation

@farhan523

@farhan523 farhan523 commented Jul 6, 2026

Copy link
Copy Markdown

Summary

Two small bug fixes in the React wrapper's lifecycle handling:

1. Editor was never destroyed on unmount

The unmount cleanup in EmailEditor.tsx closed over editor from the first render, which is always null:

useEffect(() => {
  return () => {
    editor?.destroy(); // editor is always null here
  };
}, []);

So destroy() was a silent no-op and every unmounted editor instance leaked (related: #272). Fixed by tracking the latest instance in a ref. Added a regression test that mounts/unmounts the editor with a mocked embed script and asserts destroy() is called — it fails on the old code and passes with the fix.

2. Broken effect dependency for event-listener props

The event-listener effect depended on Object.keys(methodProps).join(','), but methodProps is an array, so this yields index strings ("0,1,2") rather than prop names. Swapping one on* prop for another (same count) was invisible to React. Changed to methodProps.join(','), which is clearly what was intended.

Testing

  • tsdx test — passes, including two new regression tests:
  • tsdx build — compiles cleanly
  • tsdx lint — no new problems introduced (one pre-existing warning removed)

- The unmount cleanup captured `editor` from the first render (always
  null), so unlayer editors were never destroyed on unmount. Track the
  latest instance in a ref so cleanup destroys the real editor.
- The event-listener effect depended on Object.keys(methodProps) which
  yields array indices ("0,1,2"), not prop names, so swapping one on*
  prop for another was not detected. Use methodProps.join(',') instead.

Adds a regression test that mounts and unmounts the editor with a
mocked embed script and asserts destroy() is called.
@farhan523 farhan523 force-pushed the fix/wrapper-lifecycle-bugs branch from 5104c5e to da000bc Compare July 6, 2026 07:42
@farhan523 farhan523 changed the title Fix editor lifecycle bugs and pin @unlayer/types Fix editor lifecycle bugs in EmailEditor Jul 6, 2026
Covers the scenario from unlayer#272: a component that is mounted and
unmounted quickly, with the embed script finishing its load afterwards.
Asserts no editor is created for the unmounted component and the late
script callback does not throw.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant