Skip to content

Commit

Permalink
fix: root.render throws if configure isn't called, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Jan 19, 2025
1 parent 4e607ca commit 76fca8f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 108 deletions.
2 changes: 1 addition & 1 deletion packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type {
} from './reconciler'
export { extend, reconciler } from './reconciler'
export type { ReconcilerRoot, GLProps, CameraProps, RenderProps, InjectState } from './renderer'
export { _roots, render, createRoot, unmountComponentAtNode, createPortal } from './renderer'
export { _roots, createRoot, unmountComponentAtNode, createPortal } from './renderer'
export type {
Subscription,
Dpr,
Expand Down
15 changes: 1 addition & 14 deletions packages/fiber/src/core/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,11 @@ export function createRoot<TCanvas extends HTMLCanvasElement | OffscreenCanvas>(
// Set locals
onCreated = onCreatedCallback
configured = true

return this
},
render(children: React.ReactNode): RootStore {
// The root has to be configured before it can be rendered
if (!configured) this.configure()

if (!configured) throw "The root has to be configured before it can be rendered, call 'configure' first!"
reconciler.updateContainer(
<Provider store={store} children={children} onCreated={onCreated} rootElement={canvas} />,
fiber,
Expand All @@ -402,17 +400,6 @@ export function createRoot<TCanvas extends HTMLCanvasElement | OffscreenCanvas>(
}
}

export function render<TCanvas extends HTMLCanvasElement | OffscreenCanvas>(
children: React.ReactNode,
canvas: TCanvas,
config: RenderProps<TCanvas>,
): RootStore {
console.warn('R3F.render is no longer supported in React 18. Use createRoot instead!')
const root = createRoot(canvas)
root.configure(config)
return root.render(children)
}

interface ProviderProps<TCanvas extends HTMLCanvasElement | OffscreenCanvas> {
onCreated?: (state: RootState) => void
store: RootStore
Expand Down
2 changes: 1 addition & 1 deletion packages/fiber/src/native/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function CanvasImpl({
return onCreated?.(state)
},
})
root.current.render(
await root.current.render(
<Bridge>
<ErrorBoundary set={setError}>
<React.Suspense fallback={<Block set={setBlock} />}>{children}</React.Suspense>
Expand Down
2 changes: 1 addition & 1 deletion packages/fiber/src/web/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function CanvasImpl({
onCreated?.(state)
},
})
root.current.render(
await root.current.render(
<Bridge>
<ErrorBoundary set={setError}>
<React.Suspense fallback={<Block set={setBlock} />}>{children}</React.Suspense>
Expand Down
1 change: 0 additions & 1 deletion packages/fiber/tests/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Array [
"getRootState",
"invalidate",
"reconciler",
"render",
"unmountComponentAtNode",
"useFrame",
"useGraph",
Expand Down
2 changes: 1 addition & 1 deletion packages/fiber/tests/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('hooks', () => {
return <group />
}

await act(async () => root.render(<Component />))
await act(async () => (await root.configure()).render(<Component />))

expect(result.camera instanceof THREE.Camera).toBeTruthy()
expect(result.scene instanceof THREE.Scene).toBeTruthy()
Expand Down
10 changes: 5 additions & 5 deletions packages/fiber/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ afterEach(async () => {

describe('createRoot', () => {
it('should return a Zustand store', async () => {
const store = await act(async () => root.render(null))
const store = await act(async () => (await root.configure()).render(null))
expect(() => store.getState()).not.toThrow()
})

Expand Down Expand Up @@ -119,7 +119,7 @@ describe('createRoot', () => {
let state: RootState = null!

await act(async () => {
state = (await root.configure({})).render(<group />).getState()
state = (await root.configure()).render(<group />).getState()
state.gl.xr.isPresenting = true
state.gl.xr.dispatchEvent({ type: 'sessionstart' })
})
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('createPortal', () => {
}

await act(async () => {
root.render(
;(await root.configure()).render(
<>
<Normal />
{createPortal(<Portal />, scene, { scene })}
Expand Down Expand Up @@ -276,12 +276,12 @@ describe('createPortal', () => {
)
}

await act(async () => root.render(<Test key={0} />))
await act(async () => (await root.configure()).render(<Test key={0} />))

expect(groupHandle).toBeDefined()
const prevUUID = groupHandle!.uuid

await act(async () => root.render(<Test key={1} />))
await act(async () => (await root.configure()).render(<Test key={1} />))

expect(groupHandle).toBeDefined()
expect(prevUUID).not.toBe(groupHandle!.uuid)
Expand Down
Loading

0 comments on commit 76fca8f

Please sign in to comment.