Maximum update depth exceeded. React limits the number of nested updates to prevent infinite loops. question
P粉218775965
P粉218775965 2024-02-25 16:30:55
0
2
287

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粉218775965
P粉218775965

reply all(2)
P粉465675962

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.

P粉674757114

I solved this problem by adding the following code:

const table = useTable({
  columns,
  data,
  autoResetHiddenColumns: false, // 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!