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

245
Views
previous state in reducers in redux with react

Here is what my reducer looks like:

export default function catalogReducer(state = initialState.catalogItems, action){
  switch (action.type) {
    case types.LOAD_CATALOG_SUCCESS:
      return {
         count:action.count,
        products :[...state['products'],
        action.catalogItems  ?
        [...action.catalogItems['products']]
          :
          []
      ]
    default:
      return state;
  }
}

What I want to do:

  1. This is a reducer for loading catalog items

  2. Items are coming from a paginated api.

  3. I need to add results from api to existing list of products

Problem:

I was hoping that state argument should would have the previous state associated but no, it is always getting value from initialState which what I initialize the state with on application startup.

Why is that? What is the way around for it?

Basically I need to have the complete set of items returned through pagination api and not just the products for a given page in products array. What I am ending up with is just the array of products from last api call as state.products always gets initialized as an empty array

Also, generally speaking I am very confused about dealing with pagination api in react and redux and am not able to find a straightforward example.

A direction would be appreciated.

This is what initialState.js file looks like:

import cookie from 'react-cookie';

export default {
 categories:[],
  sessionId: cookie.load('sessionId') || '',
  youMayLikeItems : [],
  catalogItems: {'products': []}
}

//This file is to centralize initialization of states in the store

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Basically, you need to remember that you shouldn't mutate state.

export default function catalogReducer(state = initialState, action) {
  switch (action.type) {
    case types.LOAD_CATALOG_SUCCESS:
      return {
        ...state,
        catalogItems: {
          count: action.count,                          
          products: [
            ...state.catalogItems['products'],
            action.catalogItems  ?
              action.catalogItems['products']
              :
              {}
          ]
        }
  default:
    return state;
  }
}

You could find more useful examples here

Also it's better to make simple states (docs) without deep objects and use Immutable.js

over 4 years ago · Santiago Trujillo 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!