How Invalidation and Reflow work
masterLayout calculations do not run every frame for performance reasons. A reflow (recalculation) is automatically triggered when:
<Flex />props change (e.g.,alignItems,size).<Box />props change (e.g.,flexGrow,margin).<Flex />or<Box />rerenders due to children differences.
Manual Reflow: If you change object properties via useFrame, react-spring, or if a <Box /> is not rerendering because it is defined in a parent component, you must manually trigger a reflow using the useReflow() hook.
// Manual reflow with useFrame
function AnimatedBox() {
const ref = useRef()
const reflow = useReflow()
useFrame(({ clock }) => {
ref.current.scale.x = 1 + Math.sin(clock.getElapsed())
reflow()
})
return (
<Box centerAnchor>
<mesh ref={ref}>
<boxBufferGeometry attach="geometry" args={[10, 10, 10]} />
</mesh>
</Box>
)
}
// Manual reflow with useEffect (when Box is outside the component)
function AnimatedBox() {
const [state, setState] = useState(true)
const reflow = useReflow()
useEffect(reflow, [state])
return (
<mesh scale={[state ? 1 : 3, 1, 1]}>
<boxBufferGeometry attach="geometry" />
</mesh>
)
}