Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux state - Add/Edit/Remove object and its properties

This is the reducer state. I need to add, update, remove the object in cartData. At the first time, cartData is empty.

const initialState = {
   fetchData: {},
   cartData: {}
}

Example:

fetchData: {
  "React":{'name': 'React'},
  "Node":{'name': 'Node'},
}

If user ADD_ITEM react book, new item is adding in the cart here.

cartData:{
  "React":{'name': 'React', 'quantity': 1}, 
}

If user Edit_ITEM react book, existing item is updating here.

cartData:{
  "React":{'name': 'React', 'quantity': 4}, 
}

If user REMOVE_ITEM react book, removing when its come to zero here.

cartData:{

}

How can we modify redux state for these actions?

Tried this: using lodash. But did't worked out correctly.

case types.ADD_ITEM:
   return { ...state, cartData: // add new item  }

case types.EDIT_ITEM:
   return { ...state, [state.cartData.name]: action.payload  }

case types.REMOVE_ITEM:
   return _.omit(state, [state.cartData.name]: action.payload)
like image 635
Balasubramanian Avatar asked Nov 07 '17 10:11

Balasubramanian


People also ask

What are 3 main concepts of Redux?

In this brief introduction to Redux, we'll go over the main concepts: reducers , actions , action creators and store .

How do I edit Redux state?

Step 1. Click Dispatcher. Step 2: Now provide which 'action type' you want to modify. Step 3: Create a payload object & modify the state which you want to modify.

What happens when Redux state changes?

In your reducer, and for all four tries, you are modifying the existing state before returning it. This results in react-redux , when checking if your state has changed, not to see any changes as both the previous and next states are pointing to the same object.

How do I create a state object with Redux reducers?

In an actual Redux example, the state object could be built by combining two separate reducers to act independently on the byHash and byId properties of the state object to simply the logic. Full disclosure: I’m not sure that this the optimum way to solve this problem.

Is it possible to delete a previous state in Redux?

Yes, the delete statement is mutating the previous state. You'll probably find this breaks time travel/undo/redo, which are core benefits of Redux to be ignored at your own peril! That's what ...state is doing (copying the properties of the previous state to the new object literal that gets returned).

What is the root value of a redux state?

The root Redux state value is almost always a plain JS object, with other data nested inside of it. Based on this information, we should now be able to describe the kinds of values we need to have inside our Redux state: First, we need an array of todo item objects.

How does Redux work?

Every Redux app has state values, creates actions to describe what happened, and uses reducer functions to calculate new state values based on the previous state and an action. Here's the contents of our app so far: What's Next? We now have some reducer logic that will update our state, but those reducers won't do anything by themselves.


1 Answers

You can use spread syntax for add and edit items and Object.keys() and reduce() for remove item.

const initialState = {
 fetchData: {},
 cartData: {}
}

function cartReducer(state = initialState, action) {
  switch(action.type) {
    case 'ADD_ITEM':
    return {...state, cartData: {...state.cartData, ...action.payload}}
    
    case 'EDIT_ITEM':
    return {...state, cartData: {...state.cartData, ...action.payload}}
    
    case 'REMOVE_ITEM':
    let newState = Object.keys(state.cartData).reduce((r, e) => {
      if(!action.payload[e]) r[e] = state.cartData[e];
      return r
    }, {})
    
    return {...state, cartData: newState}
    
    default:
    return state;
  }
}

var state = {}

state = cartReducer(undefined, {
  type: 'ADD_ITEM',
  payload: {"React":{'name': 'React', 'quantity': 1}}
})
console.log(state)

state = cartReducer(state, {
  type: 'ADD_ITEM',
  payload: {"Node":{'name': 'Node', 'quantity': 2}}
})
console.log(state)

state =  cartReducer(state, {
  type: 'EDIT_ITEM',
  payload: {"React":{'name': 'React', 'quantity': 4}}
})
console.log(state)

state = cartReducer(state, {
  type: 'REMOVE_ITEM',
  payload: {"React":{'name': 'React', 'quantity': 1}}
})
console.log(state)
like image 140
Nenad Vracar Avatar answered Oct 15 '22 08:10

Nenad Vracar