TransWikia.com

infinite loop while using useEffect and redux store

Stack Overflow Asked by Susanta on December 20, 2021

I have a component which renders tabs based on an array in redux store and also updates the store based on the user click on tab. But it is causing infinite loop when I click the tab.

function TabsList() {
  const reportNamesStore = useSelector(getStoreTabs);
  const [reportNames, setReportNames] = useState([]);

  const handleClick = (index) => {
            dispatch(tabClicked({ active: index }))
            setReportNames([...reportNamesStore]);
  };

  useEffect(() => {
    setReportNames([...reportNamesStore]);
  }, [reportNamesStore]);

  return (
       ...
      <a onClick={() => handleClick(index)}/>
   )
 
enter code here

And my reducer is as below

getTabsReducer(
   state = { active: -1, tabs: [] },
  ...
case TAB_CLICKED:
  return {
    tabs: [...state],
    active: action.payload.active,
  };

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP