-
Notifications
You must be signed in to change notification settings - Fork 216
Fix controls rendering in StrictMode with R3F Canvas #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 4fcc479:
|
Co-authored-by: gsimone <[email protected]>
Update useToggle hook to properly handle React StrictMode's double-invocation of effects. Use prevToggledRef to track actual state changes and prevent animation on initial render, fixing controls not rendering correctly inside R3F Canvas with StrictMode enabled. Co-authored-by: gsimone <[email protected]>
Co-authored-by: gsimone <[email protected]>
yarn.lock
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot what is this doing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was an empty file accidentally created during dependency installation attempts. Removed in commit 2867f9f.
Co-authored-by: gsimone <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Please add a story with this repro
|
@copilot please add a storybook with a repro |
Add comprehensive Storybook story demonstrating the StrictMode fix with async component mounting and nested folders. Shows controls render correctly in StrictMode with delayed content layout, reproducing and validating the fix for issue #552. Co-authored-by: gsimone <[email protected]>
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Comment |
|
@copilot do use react-three-fiber to create a realistic reproduction in one of the stories. Add this to the repros story group citing the original issue like the other stories in the category |
- Add @react-three/fiber and three as dev dependencies - Create realistic StrictMode reproduction with R3F Canvas in bug-repros.stories.tsx - Remove old StrictMode.stories.tsx in favor of the new R3F-based repro - Story demonstrates issue #552 with actual R3F Canvas component - Follows the pattern of other bug repro stories with issue number citation Co-authored-by: gsimone <[email protected]>
Added R3F Canvas reproduction in commit 860b265. The story is now in the |
Fix Controls rendering in StrictMode inside R3F Canvas
Problem:
Controls added from components inside
@react-three/fiberCanvas do not render correctly when React StrictMode is enabled. The issue is related to CSS height calculation that happens prematurely.Root Cause:
The
useTogglehook in/packages/leva/src/hooks/useToggle.tsusesgetBoundingClientRect()to calculate content height for collapse/expand animations. In React 18+ StrictMode, effects run twice during initial mount (mount → cleanup → remount).The original implementation used a
firstRenderboolean ref that would be set tofalseon the first effect run. This caused the second effect run (during StrictMode's remount) to execute the height calculation logic prematurely, before the R3F Canvas content was fully laid out, resulting in incorrect height values.Solution:
Replaced the
firstRenderboolean ref with aprevToggledRefthat tracks the previous toggle state:prevToggledRef.currentisnull(initial render), skip animation logic entirelyprevToggledRef.currentequals the currenttoggledvalue, skip animation (handles StrictMode's double-invocation)toggledactually changes from the previous value, run the height calculation and animationThis ensures:
toggled=true: No height is calculated/set, wrapper uses natural height ✅toggled=false:useLayoutEffectsets height to 0 (collapsed state) ✅Changes Made:
useTogglehook's height calculation logic@react-three/fiberusage@react-three/fiberandthreeas dev dependencies for the reproduction storyTesting:
The fix is minimal and surgical, changing only the logic that determines when to run the animation. It maintains backward compatibility with existing behavior while properly handling StrictMode's double-invocation pattern. The change ensures controls render correctly in both development (with StrictMode) and production environments, particularly when used inside R3F Canvas components.
A new story has been added to
Dev/BugReprothat demonstrates:Fixes #552
Original prompt
Canvasnot rendering inStrictMode#552💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.