You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have verified that I use latest version of all @mantine/* packages
What version of @mantine/* packages do you have in package.json?
9.0.0
What package has an issue?
@mantine/core
What framework do you use?
Vite
In which browsers you can reproduce the issue?
Chrome
Describe the bug
Bug description
SegmentedControl crashes with React error #185 ("Too many re-renders") in production builds when the component is rendered inside a Modal (or any conditionally mounted
container). The crash does not occur in development builds.
Environment
@mantine/core: 9.0.0
react: 19.2.4
Bundler: Vite (ESM output)
Mode: production build only
Steps to reproduce
Render a SegmentedControl inside a <Modal> (controlled, opened state)
functionExample(){const[opened,setOpened]=useState(false);const[value,setValue]=useState('a');return(<><ButtononClick={()=>setOpened(true)}>Open</Button><Modalopened={opened}onClose={()=>setOpened(false)}><SegmentedControlvalue={value}onChange={setValue}data={['a','b','c']}/></Modal></>);}RootcauseThebug is inesm/components/SegmentedControl/SegmentedControl.mjs.ThecomponentusesuseStatetotrackDOMelementrefsandafunctionalupdaterthatalwayscreatesanew
object:
// SegmentedControl.mjs (ESM — used by Vite in production)const[refs,setRefs]=useState({});constsetElementRef=(element,val)=>{setRefs((prev)=>({ ...prev,[val]: element}));// ← always new object};// inline ref callback — new function identity on every render
ref: (node)=>setElementRef(node,`${item.value}`),Theinfiniteloopmechanism:
1.SegmentedControlrenders→refcallbackattaches→setRefscalledwithanewobject→stateupdate→re-render2.Onre-render,theinlinerefcallbackhasanewfunctionidentity3.Reactseestherefchanged→callsold_ref(null)→setRefs→stateupdate4.Reactcallsnew_ref(element)→setRefs→stateupdate5.Bothupdatestriggerre-renders→repeat→Reacthitsthe25re-renderlimit→error #185Note: theCJSversion(cjs/components/SegmentedControl/SegmentedControl.cjs)doesNOThavethisbug.Itusesdirectmutationinstead:
// SegmentedControl.cjs (CJS — does NOT crash)constsetElementRef=(element,val)=>{refs[val]=element;setRefs(refs);// ← same object reference → React bails out → no re-render};ThisESM/CJSinconsistencyconfirmsit's a regression in the ESM build.
### Ifpossible,includealinktoacodesandboxwithaminimalreproduction_Noresponse_
### PossiblefixReplacetheuseState-basedreftrackingwithuseRef+stablecachedcallbacks,soReactneverseesachangedrefidentity:
import{ createElement, useState, useRef }from"react";// Replace:// const [refs, setRefs] = useState({});// const setElementRef = (element, val) => {// setRefs((prev) => ({ ...prev, [val]: element }));// };// With:constrefsMap=useRef({});const[,forceUpdate]=useState(0);constcallbackCache=useRef({});constgetStableRef=(val)=>{if(!callbackCache.current[val]){callbackCache.current[val]=(node)=>{if(refsMap.current[val]!==node){refsMap.current[val]=node;if(node!==null)forceUpdate((n)=>n+1);}};}returncallbackCache.current[val];};// Then in the label element:// ref: (node) => setElementRef(node, `${item.value}`),// →// ref: getStableRef(`${item.value}`),// And in FloatingIndicator:// target: refs[`${_value}`],// →// target: refsMap.current[`${_value}`],Thisensures:
-Therefcallbackfunctionisstable(sameidentitybetweenrenders)-Reactnevertriggerstheold/newrefcleanupcycleunnecessarily-forceUpdateonlyfireswhenanelementactuallyattaches(node!==null), never oncleanup
### Self-service-[]Iwouldbewillingtoimplementafixforthisissue
Dependencies check up
What version of @mantine/* packages do you have in package.json?
9.0.0
What package has an issue?
@mantine/core
What framework do you use?
Vite
In which browsers you can reproduce the issue?
Chrome
Describe the bug
Bug description
SegmentedControlcrashes with React error #185 ("Too many re-renders") in production builds when the component is rendered inside aModal(or any conditionally mountedcontainer). The crash does not occur in development builds.
Environment
@mantine/core: 9.0.0react: 19.2.4Steps to reproduce
SegmentedControlinside a<Modal>(controlled,openedstate)