Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
Maximum call stack size exceeded, on click dispatch

I'm using redux toolkit to add a object to my array , for that i'm dispatching a function but i'm getting this error (i'm importing data into my details file by help of state and params):

" Details.js:16 Uncaught RangeError: Maximum call stack size exceeded at addToCart (Details.js:16:1) "

here is my details file

import {react,useEffect} from 'react';
import { useParams } from 'react-router-dom';
import {useDispatch,useSelector} from 'react-redux';
import {getProduct,addtocart} from '../features/productSlice'


function Details(){
    const params = useParams();
    const dispatch = useDispatch();

    useEffect(()=>{
        dispatch(getProduct(params.id))
    },[])

    function addToCart(){
        dispatch(addToCart(id));
    }

    const productdetail = useSelector((state)=> state.myproduct.product)
    const {title,image,description,price,id}= productdetail;

   console.log(params)
    return(
        <>
        <div className='container'>
             <img src={image}/>
            <h1>{title}</h1>
            <h5>{description}</h5>
            <h4>{price}</h4> 
        </div>
        <button 
            className='btn btn-warning'
            onClick={()=>{addToCart({id}) } 
        }
            > Add to cart </button>
         </>
    )
}

export default Details;

and here is my portion of Slice


    const productSlice = createSlice({
        name:"myproduct",
        initialState,
        reducers:{
            getProduct:(state,action)=>{
                state.product = state.products.find((el) => el.id == action.payload)
            },
            addtocart:(state,action)=>{
                state.cart = state.cart.push(action.payload)
            },
            removefromcart:(state,action)=>{
                state.cart = action.payload
            }
        },

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should work

function addToCart({ id } = {}) {
    // dispatch(addToCart(id)); <- wrong, calls itself untill exceeds max call stack size throwing a RangeError
    dispatch(addtocart({ payload: id })); // i expect this to work, because "addtocart" is the action you're importing from "productSlice"
}

sidenotes:

  • i would pass simply id instead of { id } to "addToCart" button handler, and wrap the id in { payload: { id } }
  • i would rename "addtocart" to "addToCart" and import it as "addToCartAction" (same goes to all other actions from productSlice)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!