Hello all!
I ran into a problem that I’ve been struggling to solve. Essentially, I have a Three.js application wrapped in a web component and exported as an NPM package. The goal is to use multiple instances of the NPM package in another application. In my app, I’m using Three Fiber for rendering and Zustand for state management.
My issue is that when I update the mesh, for example by applying a new material, and I use invalidate
from useThree
, the invalidate only triggers a re-render in one instance—the one that rendered first. The second instance isn’t affected.
I’ve tried several alternatives to invalidate
, such as using gl.render(scene, camera)
or advance()
, but nothing changes. Only the first instance updates. From debugging, I can see that the useThree
store instances are different, the GL
reference is not the same, and the canvases, cameras, and scenes are separate. I also checked the Zustand store, and the instances are different there as well.
Example of what i want to achieve:
Web component:
import React from ‘react’
import * as ReactDOMClient from ‘react-dom/client’
import reactToWebComponent from ‘react-to-webcomponent’
import { ExtendedR2WCOptions } from ‘extended-custom-types/extended-web-component-options’
import App from ‘./App’
export const createWeb3DEngine = (tagName: string = ‘web-3d-engine’) => {
if (!customElements.get(tagName)) {
customElements.define(
tagName,
reactToWebComponent(App, React, ReactDOMClient, {
props: {
…some props
},
} as ExtendedR2WCOptions),
)
}
}
the usage of the app in hosted app:
I wondered may be the issue is with the web component, so I tried to run the two instances uder the same root:
const MultiAppContainer = () => {
return (
<App {…testPropsFromAppLayer} />
<App {…{ …testPropsFromAppLayer }} />
)
}
ReactDOM.createRoot(document.getElementById(‘root’)!).render()
but with no luck.
I created a sandbox app where I reproduced the issue:
https://codesandbox.io/p/sandbox/multiple-views-with-uniform-controls-forked-pd3q8d
Has anyone encountered this issue and can provide some insight into what I can do?