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 a component called Tabs that contains three tabs.
When the user clicks on the Tab analytics, I want to display the data that comes from API.
The problem is when the user clicks each time on that Tab though no state is changed, it fetches the data again.
CODE
"use client"import{solarAnalyticsApiUrl,standardApiHeader}from"@/constants/urls"import{fetcher}from"@/utils/fetcher"import{Tab,TabGroup,TabList,TabPanel,TabPanels}from"@tremor/react"import{Table}from"lucide-react"import{Map}from"lucide-react"importdynamicfrom"next/dynamic"import{useParams,useSearchParams}from"next/navigation"import{HTMLAttributes,useCallback,useEffect,useState}from"react"import{useDispatch,useSelector}from"react-redux"importuseSWRMutationfrom"swr/mutation"import{RootState}from"@/redux/store"import{initSelectedRoofs,Roof}from"../state/slice"import{Analytics}from"./analytics"import{CustomInstallationParamsForm}from"./form"import{CustomInstallationImage}from"./image"import{Stats}from"./stats"exportconstTabs=({ image, roofs }: {roofs: Roof[];image: string})=>{constselectedRoofs: Roof[]=useSelector((state: RootState)=>state.customInstallation.selectedRoofs)const[data,setData]=useState<any>()constdispatch=useDispatch()constsearchParams=useSearchParams()constparams=useParams()constelectricity=searchParams.get("electricity")constslp=searchParams.get("slp")constwp=searchParams.get("wp")consturl=`${solarAnalyticsApiUrl}?address_id=${params.id}&slp_type=${slp}&annual_energy_consumption=${electricity}&wp=${wp}`const{ trigger, isMutating }=useSWRMutation(url,(url)=>fetcher(url,standardApiHeader,"PUT",{panel_placement: selectedRoofs.map((roof)=>{return{roof_id: roof.roof_id,additionalPanels:
roof.additionalPanels!==undefined ? roof.additionalPanels : roof.default_additional_panels,}}),}))// I want to run this one only if there's a change in selectedRoofs.// Though there's no change in slectedRoofs, it fetches the data again.// I tried the useCallback but it does not work.constgetData=()=>{trigger().then((data)=>{setData(data)})}useEffect(()=>{dispatch(initSelectedRoofs(roofs))},[])return(<TabGroup><TabListclassName='flex gap-x-4'><Tabicon={Map}>Modulansicht</Tab><Tabicon={Table}>Statistiken</Tab><Tabicon={Table}onClick={getData}>
Analytics
</Tab></TabList><TabPanels><TabPanel><divclassName='flex gap-16 mt-8'><CustomInstallationImageimage={image}/><CustomInstallationParamsFormroofs={roofs}/></div></TabPanel><TabPanel><divclassName='overflow-y-auto max-h-full'><Stats/></div></TabPanel><TabPanel><divclassName='overflow-y-auto max-h-full'><Analyticsdata={data}isMutating={isMutating}/></div></TabPanel></TabPanels></TabGroup>)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi Swr Team,
I have a component called Tabs that contains three tabs.
When the user clicks on the Tab analytics, I want to display the data that comes from API.
The problem is when the user clicks each time on that Tab though no state is changed, it fetches the data again.
CODE
Beta Was this translation helpful? Give feedback.
All reactions