Skip to content

Fix array signals when used as jsx #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2025
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/cuddly-geese-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals": patch
---

Fix array signals when used as jsx
7 changes: 5 additions & 2 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
return s === 0 ? 0 : s === true ? "" : s || "";
});

const isText = computed(() => !isValidElement(wrappedSignal.value));

const isText = computed(
() =>
!Array.isArray(wrappedSignal.value) &&
!isValidElement(wrappedSignal.value)
);
// Update text nodes directly without rerendering when the new value
// is also text.
const dispose = effect(function (this: Effect) {
Expand Down
22 changes: 22 additions & 0 deletions packages/preact/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,26 @@ describe("@preact/signals", () => {

expect(renderSpy).to.be.calledTwice;
});

it("Array based signals should maintain reactivity", () => {
const count = signal([1, 2, 3].map(i => <div>{i}</div>));

function App() {
return <div>{count}</div>;
}

act(() => {
render(<App />, scratch);
});
expect(scratch.innerHTML).to.equal(
"<div><div>1</div><div>2</div><div>3</div></div>"
);

act(() => {
count.value = [4, 5, 6].map(i => <div>{i}</div>);
});
expect(scratch.innerHTML).to.equal(
"<div><div>4</div><div>5</div><div>6</div></div>"
);
});
});