Skip to content

Commit

Permalink
WIP on v3: 88b4b04 chore: sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperskei committed Aug 26, 2024
1 parent 88b4b04 commit 30cc5fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
27 changes: 27 additions & 0 deletions packages/jsx/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,31 @@ test('ref unmount callback', async () => {
assert.is(ref, null)
})

test('child ref unmount callback', async () => {
const { h, hf, parent, mount, window } = setup()

const Component = (props: JSX.HTMLAttributes) => <div {...props} />

let ref: null | HTMLElement = null

const component = (
<Component
ref={(ctx, el) => {
ref = el
return () => {
ref = null
}
}}
/>
)

mount(parent, component)
assert.instance(ref, window.HTMLElement)
await sleep()

ref!.remove()
await sleep()
assert.is(ref, null)
})

test.run()
6 changes: 5 additions & 1 deletion packages/jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ export const reatomJsx = (ctx: Ctx, DOM: DomApis = globalThis.window) => {
if (k === 'ref') {
ctx.schedule(() => {
const cleanup = prop(ctx, element)
if (typeof cleanup === 'function') unlink(element, cleanup)
if (typeof cleanup === 'function') {
let list = unsubscribesMap.get(element)
if (!list) unsubscribesMap.set(element, (list = []))
unlink(element, cleanup)
}
})
} else if (isAtom(prop) && !prop.__reatom.isAction) {
if (k.startsWith('model:')) {
Expand Down

0 comments on commit 30cc5fa

Please sign in to comment.