fix(react-form-devtools): re-mount Solid component on theme change (closes #2357) #2371
Opendikshit-n(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/2371/headopened - packages/react-form-devtools/src/FormDevtools.tsx line 38Resolvedby dikshit-n(GitHub profile, opens in a new tab)@@ -10,0 +15,21 @@15+ * called again. The Solid component receives props.theme as a plain (non-reactive) value16+ * and never re-renders.17+ *18+ * Fix: use a ref to track the previous theme. Whenever the theme changes, the cleanup19+ * runs (unmounting the old Solid component) and then the effect body runs again, calling20+ * mount() with the updated props. This ensures the Solid Devtools component always21+ * starts fresh with the correct theme value.22+ */23+function FormDevtoolsPanel(props: DevtoolsPanelProps) {24+ const devToolRef = useRef<HTMLDivElement>(null)25+ const devtools = useRef<InstanceType<typeof FormDevtoolsCore> | null>(null)26+ const prevThemeRef = useRef<string | undefined>(undefined)27+28+ // theme is passed by TanStack DevTools outer shell via props.29+ // We use type assertion because @tanstack/devtools types are not available30+ // as a direct dependency of this package.31+ const theme = (props as { theme?: string }).theme32+33+ useEffect(() => {34+ // Skip if theme hasn't actually changed35+ if (theme === prevThemeRef.current) returnpackages/react-form-devtools/tests/formDevtools.spec.tsx lines 14 to 18OutdatedResolvedby dikshit-n(GitHub profile, opens in a new tab)@@ -2,4 +1,18 @@1+import { describe, expect, it, vi } from 'vitest'223-describe('test suite', () => {4- it('should work', () => {5- expect(true).toBe(true)3+// Mock FormDevtoolsCore so we can verify mount/unmount calls without4+// needing a real DOM environment5+vi.mock('@tanstack/form-devtools', () => {6+ class MockFormDevtoolsCore {7+ mount = vi.fn()8+ unmount = vi.fn()9+ }10+ return { FormDevtoolsCore: MockFormDevtoolsCore }11+})12+13+describe('FormDevtoolsPanel mount/unmount contract', () => {14+ it('FormDevtoolsCore is imported and can be instantiated', async () => {15+ const { FormDevtoolsCore } = await import('@tanstack/form-devtools')16+ const instance = new FormDevtoolsCore() as InstanceType<typeof FormDevtoolsCore>17+ expect(typeof instance.mount).toBe('function')18+ expect(typeof instance.unmount).toBe('function')
- packages/react-form-devtools/src/FormDevtools.tsx line 24OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -7,4 +6,19 @@76 export interface FormDevtoolsReactInit extends DevtoolsPanelProps {}879-const [FormDevtoolsPanel, FormDevtoolsPanelNoOp] =10- createReactPanel(FormDevtoolsCore)8+/**9+ * Fixed React panel wrapper for FormDevtoolsCore.10+ *11+ * Root cause of #2357 ("devtools are always light mode even if TanStackDevtools says dark"):12+ * The original createReactPanel hook only calls mount() once on the Solid FormDevtoolsCore13+ * class. When TanStack DevTools outer shell switches theme, it calls14+ * plugin.render(el, newTheme) which creates a new React element — but mount() is never15+ * called again. The Solid component receives props.theme as a plain (non-reactive) value16+ * and never re-renders.17+ *18+ * Fix: track the previous theme in a ref. The effect dependency is [theme] only — it19+ * fires only when the theme value changes, never on unrelated prop changes. The ref20+ * guards against the initial mount where prevThemeRef.current is undefined (matching21+ * an undefined theme on first render). Cleanup unmounts the old Solid instance before22+ * the next mount with the updated props.23+ */24+function FormDevtoolsPanel(props: DevtoolsPanelProps) {packages/react-form-devtools/src/FormDevtools.tsx line 44OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -10,0 +24,21 @@24+function FormDevtoolsPanel(props: DevtoolsPanelProps) {25+ const devToolRef = useRef<HTMLDivElement>(null)26+ const devtools = useRef<InstanceType<typeof FormDevtoolsCore> | null>(null)27+ const prevThemeRef = useRef<string | undefined>(undefined)28+29+ // theme is passed by TanStack DevTools outer shell via props.30+ // We use type assertion because @tanstack/devtools types are not available31+ // as a direct dependency of this package.32+ const theme = (props as { theme?: string }).theme33+34+ useEffect(() => {35+ // Guard: skip if theme hasn't actually changed (ref was already updated36+ // in the prior effect run, or this is the very first render with undefined).37+ if (theme === prevThemeRef.current) return38+ prevThemeRef.current = theme39+40+ if (!devToolRef.current) return41+42+ devtools.current?.unmount()43+ devtools.current = new FormDevtoolsCore()44+ devtools.current.mount(devToolRef.current, props)
- packages/react-form-devtools/tests/formDevtools.spec.tsx line 80Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -5,0 +60,21 @@60+ expect(firstInstance!.unmount).not.toHaveBeenCalled()61+62+ unmount()63+ })64+65+ it('unmounts the old instance and mounts a new one when theme changes', () => {66+ const { rerender, unmount } = render(<FormDevtoolsPanel theme="light" />)67+ const lightInstance = lastInstance.current68+ expect(lightInstance?.mount).toHaveBeenCalledTimes(1)69+ expect(lightInstance?.mount).toHaveBeenCalledWith(70+ expect.any(HTMLDivElement),71+ expect.objectContaining({ theme: 'light' }),72+ )73+74+ rerender(<FormDevtoolsPanel theme="dark" />)75+76+ // After theme change: old instance unmounted, new instance mounted.77+ expect(lightInstance!.unmount).toHaveBeenCalledTimes(1)78+ const darkInstance = lastInstance.current79+ expect(darkInstance).not.toBe(lightInstance)80+ expect(darkInstance?.mount).toHaveBeenCalledTimes(1)
dikshit-n(GitHub profile, opens in a new tab) linked this to fix #2357
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Team
Run ID:
d74b4533-669b-48ac-8945-591ddc04f392📥 Commits
Reviewing files that changed from the base of the PR and between e18edf1008065055944123766c8f2e0361d4e975 and c8f1f0a6e27ea4d050d8704c554380b569bed3a5.
📒 Files selected for processing (1)
packages/react-form-devtools/tests/formDevtools.spec.tsx🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
FormDevtoolsPanelnow manages theFormDevtoolsCorelifecycle directly and recreates it when the theme changes. The plugin documents the fix, and tests verify mounting, unmounting, remounting, and no-op rendering.Changes: Form Devtools Theme Lifecycle
packages/react-form-devtools/src/FormDevtools.tsx,packages/react-form-devtools/src/plugin.tsxFormDevtoolsPanelmounts and unmountsFormDevtoolsCorethroughuseEffect, recreates it when the theme changes, and provides a standalone no-op panel. The plugin documents the theme update fix.packages/react-form-devtools/tests/formDevtools.spec.tsxFormDevtoolsCoreand verify initial mounting, unchanged-theme reuse, theme remounting, teardown, and no-op rendering.Estimated code review effort: 3 (Moderate) | ~20 minutes
Sequence Diagram(s)#
Merge Risk: 🟡 Moderate · up to
8edc8Theme-related rerenders may leave the Form DevTools panel blank even when the theme has not changed. This should be corrected before merge to preserve panel availability.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#2357by remounting the Form DevTools component when the outer TanStack DevTools theme changes. The implementation passes the updated theme and includes integration tests for…#2357. The component export adjustments, lifecycle cleanup, documentation comments, and integration tests directly support the theme synchronization fix.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share