1

there is a state like this:

 review: {
    "amount": 217,
    "snippets": [
      {
       "id": 0,
       "flag": false
      },
      {
       "id": 1,
       "flag": false
      },
      ...
    ]
  }

Now if I only want to set the review.snippets[1].flag = true, what should write in the reducer? For example:

case SET_FLAG: return {
    ...state,
    review: {
      ...state.review,
      snippets: {
        ...state.review.snippets,
        // don't know how to write here to express array
      }
    },
};
user6557921
  • 55
  • 1
  • 3

1 Answers1

0

If you're not using combineReducers to separate pieces of your store you could keep your reducer case similar but do an operation like this to the snippets array

// grab your snippets
const snippets = state.review.snippets

// create a copy
const snippetsCopy = snippets.slice()

// replace the element with a new object with the same properties
// and overwrite the property you want
snippetsCopy[1] = {
  ...snippetsCopy[1],
  flag: true
}
const newSnippetsArray = Object.assign(arr, newArr)

// in your switch case return something like this:

return {
    ...state,
    review: {
      ...state.review,
      snippets: newSnippetsArray
    },
};
DDS
  • 4,325
  • 22
  • 33
jmancherje
  • 6,439
  • 8
  • 36
  • 56