0

Here is the problem function in my redux slice:

setButtonBackgroundColor: (state, action) => {
      const btn = state.list.present.find(
        (btn) => btn.id === action.payload.id
      );
      let copy = Object.assign({}, btn);
      state.list.past = [...state.list.past, copy];
      btn.style.backgroundColor = action.payload.backgroundColor;
    }

When btn.style.backgroundColor is updated, so is copy. Any idea why?

jbcortez89
  • 75
  • 7
  • 3
    Because you're only making a _shallow_ copy. – jonrsharpe Oct 12 '21 at 22:48
  • 1
    Does this answer your question? [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – pilchard Oct 12 '21 at 22:48
  • @jonrsharpe Thanks! JSON.parse(JSON.stringify(btn)) did the trick. – jbcortez89 Oct 12 '21 at 22:55

0 Answers0