How to access parent data in useFrame #648
runemadsen
started this conversation in
General
Replies: 1 comment 4 replies
-
not sure what you mean when you say it re-renders the whole sketch or that you can't rely on props --- why can't you? if you pass a fresh prop it re-renders the component, that's react. if you don't want that to happen indiscriminately there's React.memo, which only renders when props actually change - but i don't see how that little flag can cause any relevant overhead here. you can of course access props in useframe, but ... to change a color? useframe runs 60 times per second, it's a loop. this is what you would do in any other react app: function App() {
const [sticky, set] = useState(false)
return (
<Sticky onChange={set}>
<Canvas>
<Box sticky={sticky} />
</Canvas>
</Sticky>
)
}
function Box({ sticky, ...props }) {
return (
<mesh {...props}>
<meshStandardMaterial attach="material" color={sticky ? "red" : "orange"} /> |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a custom component called
Sticky
which I wrap around myCanvas
to make it stick to the top of the screen during scroll (with CSSposition: sticky
). This component has anonChange
callback that gets called when the canvas is sticky or not.My
Box
component renders a THREE.jsboxBufferGeometry
, but I want the box to change color whenever the canvas is sticky. I basically need to pass theSticky
state to theBox
component so I can use this inside theuseFrame
hook.However, if I do what I'd normally do with React and pass this as a prop to
Box
, it will re-render the entire sketch, and I don't want this to happen. The question here is how do I access data from the parent inside areact-three-fiber
component without re-rendering the whole damn thing? I've triedReact.createContext
, but that will also re-render the component. The only solution I can think of is to rely on a global variable, but this defeats the purpose of React and makes it impossible to use multiple instances of the component.Or said another way: If I can't rely on props to update the Three sketch, how do I pass data from the parent to the THREE sketch?
Beta Was this translation helpful? Give feedback.
All reactions