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

feat(core): add onMount instance callback #2308

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 7 additions & 1 deletion packages/fiber/src/core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type BaseInstance = Omit<THREE.Object3D, 'children' | 'attach' | 'add' |
remove: (...object: Instance[]) => Instance
add: (...object: Instance[]) => Instance
raycast?: (raycaster: THREE.Raycaster, intersects: THREE.Intersection[]) => void
onUpdate?: (self: Instance) => void
onMount?: (self: Instance) => void
}
export type Instance = BaseInstance & { [key: string]: any }

Expand Down Expand Up @@ -272,6 +274,8 @@ function createRenderer<TCanvas>(roots: Map<TCanvas, Root>, getEventPriority?: (
}
}
})

newInstance.onMount?.(newInstance)
}

const reconciler = Reconciler({
Expand Down Expand Up @@ -301,7 +305,7 @@ function createRenderer<TCanvas>(roots: Map<TCanvas, Root>, getEventPriority?: (
const localState = (instance?.__r3f ?? {}) as LocalState
// https://github.com/facebook/react/issues/20271
// Returning true will trigger commitMount
return !!localState.handlers || !!localState.attach
return !!localState.handlers || !!localState.attach || !!instance.onMount
},
prepareUpdate(instance: Instance, type: string, oldProps: any, newProps: any) {
// Create diff-sets
Expand Down Expand Up @@ -352,6 +356,8 @@ function createRenderer<TCanvas>(roots: Map<TCanvas, Root>, getEventPriority?: (
attach(parent, instance, localState.attach)
}
}

instance.onMount?.(instance)
},
getPublicInstance: (instance: Instance) => instance,
shouldDeprioritizeSubtree: () => false,
Expand Down
1 change: 1 addition & 0 deletions packages/fiber/src/three-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface NodeProps<T, P> {
ref?: React.Ref<T>
key?: React.Key
onUpdate?: (self: T) => void
onMount?: (self: T) => void
}

export type ExtendedColors<T> = { [K in keyof T]: T[K] extends THREE.Color | undefined ? Color : T[K] }
Expand Down
31 changes: 31 additions & 0 deletions packages/fiber/tests/core/renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,35 @@ describe('renderer', () => {
const respectedKeys = privateKeys.filter((key) => overwrittenKeys.includes(key) || state[key] === portalState[key])
expect(respectedKeys).toStrictEqual(privateKeys)
})

it('should safely call onMount', async () => {
let safe = false

const Test = (props: any) => {
safe = false
return (
<group
{...props}
attach={(_, self: any) => ((self.attached = true), () => (self.attached = false))}
onMount={(self: any) => void (safe = self.attached)}
/>
)
}

// Mount
await act(async () => root.render(<Test args={[0]} />))
expect(safe).toBe(true)

// Reconstruct
await act(async () => root.render(<Test args={[1]} />))
expect(safe).toBe(true)

// Update
await act(async () => root.render(<Test args={[1]} foo />))
expect(safe).toBe(false)

// Unmount
await act(async () => root.render(null))
expect(safe).toBe(false)
})
})