Skip to content
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

fix(jsx): preserve the state of element even if it is repeatedly evaluated by children #2563

Merged
merged 2 commits into from
Apr 30, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions deno_dist/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ const buildNode = (node: Child): Node | undefined => {
return [node.toString(), true] as NodeString
} else {
if (typeof (node as JSXNode).tag === 'function') {
if ((node as NodeObject)[DOM_STASH]) {
node = { ...node } as NodeObject
}
;(node as NodeObject)[DOM_STASH] = [0, []]
} else {
const ns = nameSpaceMap[(node as JSXNode).tag as string]
Expand Down
65 changes: 62 additions & 3 deletions src/jsx/dom/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDOM } from 'jsdom'
import type { FC } from '..'
import type { FC, Child } from '..'
// run tests by old style jsx default
// hono/jsx/jsx-runtime and hono/jsx/dom/jsx-runtime are tested in their respective settings
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -771,14 +771,73 @@ describe('DOM', () => {
'<div><p>Count: 0</p><button>+</button><div><p>Child Count: 0</p><button>+</button></div></div>'
)
const [button, childButton] = root.querySelectorAll('button')
for (let i = 0; i < 5; i++) {
for (let i = 0; i < 3; i++) {
childButton.click()
await Promise.resolve()
}
for (let i = 0; i < 2; i++) {
button.click()
await Promise.resolve()
}
for (let i = 0; i < 3; i++) {
childButton.click()
await Promise.resolve()
}
for (let i = 0; i < 3; i++) {
button.click()
await Promise.resolve()
}
expect(root.innerHTML).toBe(
'<div><p>Count: 5</p><button>+</button><div><p>Child Count: 6</p><button>+</button></div></div>'
)
})

it('nested useState() with children', async () => {
const ChildCounter = () => {
const [count, setCount] = useState(0)
return (
<div>
<p>Child Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
)
}
const Counter = ({ children }: { children: Child }) => {
const [count, setCount] = useState(0)
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+</button>
{children}
</div>
)
}
const app = (
<Counter>
<ChildCounter />
</Counter>
)
render(app, root)
expect(root.innerHTML).toBe(
'<div><p>Count: 0</p><button>+</button><div><p>Child Count: 0</p><button>+</button></div></div>'
)
const [button, childButton] = root.querySelectorAll('button')
for (let i = 0; i < 3; i++) {
childButton.click()
await Promise.resolve()
}
for (let i = 0; i < 2; i++) {
button.click()
await Promise.resolve()
}
for (let i = 0; i < 6; i++) {
for (let i = 0; i < 3; i++) {
childButton.click()
await Promise.resolve()
}
for (let i = 0; i < 3; i++) {
button.click()
await Promise.resolve()
}
expect(root.innerHTML).toBe(
'<div><p>Count: 5</p><button>+</button><div><p>Child Count: 6</p><button>+</button></div></div>'
)
Expand Down
3 changes: 3 additions & 0 deletions src/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ const buildNode = (node: Child): Node | undefined => {
return [node.toString(), true] as NodeString
} else {
if (typeof (node as JSXNode).tag === 'function') {
if ((node as NodeObject)[DOM_STASH]) {
node = { ...node } as NodeObject
}
;(node as NodeObject)[DOM_STASH] = [0, []]
} else {
const ns = nameSpaceMap[(node as JSXNode).tag as string]
Expand Down