0

I have the following state update function for a redux array

if (replace) {
    return {
      ...state,
      mySelectionOtrosFiltros: items
    }
  } else {
    return {
      ...state,
      mySelectionOtrosFiltros: state.mySelectionOtrosFiltros.concat(a[0])
    }
  }

If replace is false, the state update works properly, and I can see it being updated.

But if repace is true, i want to substitute mySelectionOtrosFiltros which is an array of objects, completly by a new array of objects named items

But that case isnt working.

I can see state.mySelectionOtrosFiltros being updated, but its not rerendered so i guess im messing up the inmutability. It will only be rendered properly if i trigger the else statement , in that case the new element is added and the previous updates are now displayed.

example of non working case

mySelectionOtrosFiltros = [{a: 1 , b: 2, c: 3}]

items = [{a: 2, b: 2, c: 2},{a:4 , b: 4, c: 4}]

myselectionotrosfiltros should be replaced with items completly, so it looks like items

return {
      ...state,
      mySelectionOtrosFiltros: items
    }

it doesnt get replaced

1
  • We can't answer that without knowing how you calculate items Commented Feb 19, 2021 at 11:44

1 Answer 1

1

you need to share full code in order to really pin which parts of the code violated immutability.

having that said you can still do a quick try by creating a new copy of items as below

if (replace) {
    return {
      ...state,
      mySelectionOtrosFiltros: [...items]
    }
  }

If indeed its due to mutation, the above code should fix your issue

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.