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

213
Views
What should be the correct inital value for redux state

I wanted to create 'add to cart' functionality using Redux, so I have defined a reducer function which accepts a state and action, where state is provided a default value.

export const defaultValue= {
  products: [
    {
      id: "",
      title: "",
      description: "",
      image: "",
      price: 0,
      amount: 0,
    },
  ]
};

export const manageCart = (
  state = defaultValue,
  action: { type: string; payload: any}
) => {
  switch (action.type) {
    case actionConstants.ADD_TO_CARTS:
      {
       // ADD to card
      }
  break;
    case actionConstants.REMOVE_FROM_CART: {
      // Remove from card     
     }
 default:
      return state;
}
}

enter image description here So by default I'm seeing this blank product on my cart which is messing up my logic moving further. Since it is mandatory to provide initial value to the state, what should i do differently to get rid of this issue.

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

0

So by default I'm seeing this blank product on my cart which is messing up my logic moving further.

Because you are passing that blank product in the default state!

Just pass an empty array instead

export const defaultValue= {
  products: []
};
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!