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
7 changes: 7 additions & 0 deletions .changeset/tidy-donuts-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@preact/signals": patch
---

Dispose signal prop updaters when an element re-renders without any signal props.

The disposal pass only ran when the new render still carried at least one signal-bound prop. When every signal prop was replaced by plain values, the old updater effect stayed subscribed and kept writing the previous signal's values straight into the DOM, overriding whatever Preact rendered.
28 changes: 17 additions & 11 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,24 @@ hook(OptionsTypes.DIFFED, (old, vnode) => {
if (typeof vnode.type === "string" && (dom = vnode.__e as Element)) {
let props = vnode.__np;
let renderedProps = vnode.props;
if (props) {
let updaters = dom._updaters;
if (updaters) {
for (let prop in updaters) {
let updater = updaters[prop];
if (updater !== undefined && !(prop in props)) {
updater._dispose();
// @todo we could just always invoke _dispose() here
updaters[prop] = undefined;
}
let updaters = dom._updaters;
if (updaters) {
// Dispose updaters for props that are no longer bound to a signal.
// This must also run when the re-render carried no signal props at
// all (`vnode.__np` is undefined), otherwise the stale updater stays
// subscribed and keeps writing the old signal's values into the DOM.
for (let prop in updaters) {
let updater = updaters[prop];
if (updater !== undefined && (!props || !(prop in props))) {
updater._dispose();
// @todo we could just always invoke _dispose() here
updaters[prop] = undefined;
}
} else {
}
}

if (props) {
if (!updaters) {
updaters = {};
dom._updaters = updaters;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/preact/test/browser/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,25 @@ describe("@preact/signals", () => {
expect(scratch.firstChild).to.have.property("checked", false);
});

it("should dispose the updater when re-rendering without any signal props", async () => {
const s = signal("bound");
// @ts-ignore
render(<input value={s} />, scratch);
expect(scratch.firstChild).to.have.property("value", "bound");

// Re-render the same element with no signal props at all.
render(<input value="plain" />, scratch);
expect(scratch.firstChild).to.have.property("value", "plain");

// The old binding must be disposed; a write to the previously bound
// signal must not clobber what Preact rendered.
act(() => {
s.value = "stale write";
});
await sleep();
expect(scratch.firstChild).to.have.property("value", "plain");
});

it("should update props without re-rendering", async () => {
const s = signal("initial");
const spy = vi.fn();
Expand Down