Skip to content

Commit

Permalink
react-native compatability fix
Browse files Browse the repository at this point in the history
fix: cause effect to trigger when ref is available
  • Loading branch information
brianzinn authored Dec 12, 2022
2 parents da4943c + 83aa182 commit 774cc98
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/react-babylonjs/src/Engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const ReactBabylonjsEngine: React.FC<EngineProps> = (props: EngineProps, context
const onEndRenderLoopObservable = useRef<Observable<Engine>>(new Observable<Engine>())

const canvasRef = useRef<Nullable<HTMLCanvasElement>>(null)
const [canvasReady, setCanvasReady] = useState(false)
const shouldRenderRef = useRef(true)

// const renderOptions: RenderOptions = props.renderOptions ?? {};
Expand All @@ -118,6 +119,10 @@ const ReactBabylonjsEngine: React.FC<EngineProps> = (props: EngineProps, context
}, [isPaused])

useEffect(() => {
if (!canvasReady) {
return;
}

if (canvasRef.current === null) {
return
}
Expand Down Expand Up @@ -183,7 +188,7 @@ const ReactBabylonjsEngine: React.FC<EngineProps> = (props: EngineProps, context
engine.current = null
}
}
}, [canvasRef])
}, [canvasReady])

const opts: any = {}

Expand All @@ -202,7 +207,7 @@ const ReactBabylonjsEngine: React.FC<EngineProps> = (props: EngineProps, context
<canvas
{...opts}
{...canvasProps}
ref={canvasRef}
ref={(view) => {canvasRef.current = view; setCanvasReady(true)}}
style={{ width: '100%', height: '100%', ...style }}
>
{engine.current !== null && props.children}
Expand Down

0 comments on commit 774cc98

Please sign in to comment.