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
The current implementation of useSWRConfig hook creates a new configuration object on every render by merging the default config with the context config. This causes unnecessary re-renders in components that depend on the config reference, even when the actual configuration values haven't changed.
The useSWRConfig hook should maintain a stable reference to the merged configuration object across re-renders when the underlying configs (default and context) haven't changed. This would prevent unnecessary re-renders in dependent components.
Repro Steps / Code Example
constswrConfig: SWRConfiguration={revalidateOnMount: true,revalidateIfStale: false,revalidateOnFocus: false,revalidateOnReconnect: false,}functionApp(){return(<SWRConfigvalue={swrConfig}><ChildComponent/></SWRConfig>)}functionChildComponent(){const[counter,setCounter]=useState(0)constswrConfig=useSWRConfig()useEffect(()=>{// This effect runs on every render because config reference changesconsole.log('Config changed')},[swrConfig])return(<buttononClick={()=>setCounter(prev=>prev+1)}>
counter + 1
</button>)}
Additional Context
SWR version: 2.3.2
I'd be happy to submit a PR with this fix if you think this is a valid issue. The PR would include:
The optimization implementation shown above
Test cases to verify the stable reference behavior
Documentation updates if needed
Let me know if you'd like me to proceed with the PR or if you have any other suggestions for improvement.
The text was updated successfully, but these errors were encountered:
Performance Optimization Suggestion
Description / Observed Behavior
The current implementation of
useSWRConfig
hook creates a new configuration object on every render by merging the default config with the context config. This causes unnecessary re-renders in components that depend on the config reference, even when the actual configuration values haven't changed.Expected Behavior
The
useSWRConfig
hook should maintain a stable reference to the merged configuration object across re-renders when the underlying configs (default and context) haven't changed. This would prevent unnecessary re-renders in dependent components.Repro Steps / Code Example
Additional Context
SWR version: 2.3.2
I'd be happy to submit a PR with this fix if you think this is a valid issue. The PR would include:
Let me know if you'd like me to proceed with the PR or if you have any other suggestions for improvement.
The text was updated successfully, but these errors were encountered: