search

Home  >  Q&A  >  body text

Maximum update depth exceeded. React limits the number of nested updates to prevent infinite loops. question

I'm trying to use the react-table library, but I've run into this problem and I don't know how to fix it.

Uncaught Error: Maximum update depth exceeded. This can happen when a component calls setState repeatedly in componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

export const GetProducts=async()=>{
    try {
        const response=await axios({
            url:`${baseUrl}/products`,
            method:"GET"
          })

          // console.log(response.data.products)
          return await response.data.products
    } catch (error) {
        console.log(error.response)
    }
}

I'm trying this:

const TablaStock = () => {
  const [data, setData] = useState([]);


  useEffect(() => {
    const getProducts = async () => {
      const response = await GetProducts();
      setData(response.products);
    };

    getProducts();
  }, [data]);


  const columns =useMemo(() =>  [
    { Header:"CODIGO",
      accessor: "codigo"
     },
    { Header:"PRENDA",
      accessor: "prenda" },
    { Header:"MARCA",
      accessor: "marca" },
    { Header:"CATEGORIA",
      accessor: "categoria" },
    { Header:"TALLE",
      accessor: "" },
    { Header:"CLIENTE",
      accessor: "cliente" },
    { Header:"FECHA DE INGRESO",
      accessor: "fechaIngreso" },
    { Header:"PRECIO DE VENTA",
      accessor: "precioVenta" },
    { Header:"GANANCIA CLIENTE",
      accessor: "" },
    { Header:"GANCANIA FERNANDEZ SHOP",
      accessor: "",
      Cell:({})},
    { Header:"ESTADO",
      accessor: "estado" },
    { Header:"TIEMPO EN VENTA",
      accessor: "tiempoEnVenta" },
  ]);

  const table=useTable({
    columns,
    data })

   

  return (
  <>
  </>
  );
};

export default TablaStock;

P粉218775965P粉218775965315 days ago395

reply all(2)I'll reply

  • P粉465675962

    P粉4656759622024-02-26 09:24:36

    You get into an infinite loop with Effect because you get the "data" from the server, but also re-render via the cahnge of that variable. Remove "data" from useEffect's dependency list:

    useEffect(() => {
        const getProducts = async () => {
          const response = await GetProducts();
          setData(response.products);
        };
    
        getProducts();
      }, []); // Remove the 'data' dependency

    Hope this helps.

    reply
    0
  • P粉674757114

    P粉6747571142024-02-26 00:44:31

    I solved this problem by adding the following code:

    const table = useTable({
      columns,
      data,
      autoResetHiddenColumns: false, // <-- stops the rerendering
      autoResetSortBy: false, //  <-- stops the rerendering
    });

    reply
    0
  • Cancelreply