score:0
Putting [firebase, cartItems] tells react to always re-render if any change is made to these two parametres. So that is why there are too many re-renders.
score:3
use blank []
instead of [firebase, cartItems]
score:3
The below code should work for you:
const cartItems = useItems();
// I assume this gives you cartItems.
let [total, updateTotal] = useState(() => {
if (cartItems) {
// item object {id: "2", name: "Cucumber", category: "vegetable", price: 50, // //count: 0}
return cartItems.reduce((acc, elem) => {
acc += elem.price * elem.count;
return acc;
}, 0);
}
return 0;
});
let [count, updateCount] = useState(cartItems.length);
React.useEffect(() => {
if (cartItems) {
updateCount(cartItems.length);
// item object {id: "2", name: "Cucumber", category: "vegetable", price: 50, // //count: 0}
const total = cartItems.reduce((acc, elem) => {
acc += elem.price * elem.count;
return acc;
}, 0);
updateTotal(total);
}
}, [cartItems]);
So, basically you need to initiate the state once you get some value from the useItems and then also need to update it when cartItems reference changes
Source: stackoverflow.com
Related Query
- Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop
- "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- Too many re-renders. React limits the number of renders to prevent an infinite loop
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- Too many re-renders. React Limits the number of renders to prevent an infinite loop - React hooks
- React function Component state - Too many re-renders. React limits the number of renders to prevent an infinite loop
- "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- error too many re-renders. react limits the number of renders to prevent an infinite loop
- why do i get Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- ..Use state error: Too many re-renders React limits the number of renders to prevent an infinite loop
- REACTJS Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- React Query with hooks is throwing error, "Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- received this error "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop [another variant]
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. React js
- How to fix Too many re-renders. React limits the number of renders to prevent an infinite loop
- React Js : Too many re-renders. React limits the number of renders to prevent an infinite loop
- Loading state in react: Too many re-renders. React limits the number of renders to prevent an infinite loop
- React Hook Form Too many re-renders. React limits the number of renders to prevent an infinite loop
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Next js error
- Too many re-renders. React limits the number of renders to prevent an infinite loop. and
- React: Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- ReactJS Too many re-renders. React limits the number of renders to prevent an infinite loop
- Use mobx and react get an error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. because of setState
- Setting Axios Response as State. Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop and it disappear when i clear the stored session data
- ReactJS, react-bootstrap, Modal Box: "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."
- getting an error like Too many re-renders. React limits the number of renders to prevent an infinite loop
- Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. custom hooks with nested functions
More Query from same tag
- Is it safe to run animations in a useEffect hook?
- Input Attribute onChange only if Props is not Undefined
- Why is my page reloading after onChange trigger update?
- My image doesn't render when I pass the image source path as a props. Image renders when I give the image source path
- react axios - losing this context despite using arrow functions
- Module not found: Can't resolve 'react-table/react-table.css'
- Unable To Verify Token Type Error React App
- Send form data from React to ASP.NET Core API
- Is there any way to change the title and save type of file dialog on electron?
- I am new to react. I want to render a child component using a single state using an onClick event in react JS
- Typescript React - Check if route path exists in array
- How to use Tooltip in react-redux
- Cannot set headers after they are sent to the client (error when i'm striking axios post requrest)
- React.js Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
- React: How to direct new page
- Correct Format in Passing Dynamic Text in JSON String for Excel Headers
- Redux toolkit typescript -- what is my PayloadAction type?
- Javascript - How to push data inside a map function?
- Is this a valid way to update the state of a Array in Array of Objects State?
- Material UI & Styled Component Overriding Issue
- I need to collapse table one row at a time .. i have used material (React JS) Collapsible table
- How to manually label a LabelList in Recharts React
- Issue getting items in array to display
- React Router Routing three levels deep, Only works in App.js file
- How could i change my state from getCellActions
- Having trouble removing white bar at bottom of screen
- I have a React component using Antd, need to style the background of every other row
- Change value of a key in array of objects in redux reducer
- my information is not being sent to my database
- How to use Apollo's multiple useQueries() hooks in React