JavaScript Immutability and updating nested key of local state immutable update nested object,immutable update nested map,immutable update

I have the following state.

state = {
  friends: {
    nickNames: ['Polly', 'P', 'Pau'],
    ... here more k:v
    },
  }
}

And I want to update nickNames array with a value coming from an uncontrolled form through a method in my class. However, I'm having issues at the time of determine if I am setting the state properly without mutating it.

I am doing the following

updateArray = (nickName) => {

  const tempDeepCopy = {
    ...this.state,
    friends: {
      ...this.state.friends,
      nickNames: [...this.state.friends.nickNames]
    }
  }

  tempDeepCopy.friends.nickNames.push(nickName)

  this.setState({
    friends: 
    {
      nickNames: tempDeepCopy.friends.nickNames
    }
  })

}

Is this the proper way of doing it? If so, is it also the most efficient given the state? I am trying to avoid helper libraries to learn how to make deep copies.

I will appreciate help since Im trying to learn immutability and it is a concept that is taking me a lot of effort.

Answer:1

setState() in class components does shallow merge.

So you could just ignore other "parent" keys and focus only on friends.

You can also simplify it like:

this.setState({
  friends: { // only focus on friends
    ...this.state.friends, // do not ignore other friend k:v pairs
    nicknames: [
      ...this.state.friends.nicknames,
      nickName
    ]
  }
})
Answer:2

Why not just;

updateArray = (nickName) => {

  const updatedNickNames = [...this.state.friends.nickNames, nickName];

  this.setState({
    friends: {
      ...this.state.friends,
      nickNames: updatedNickNames
    }
  });

}

Because nickNames is just a array of strings, you can copy it with the spread operator. Also, with setState you can change a specific part of your state, in your case you only have to worry about the friends part.

Answer:3

You can just use

this.setState({
    friends: 
    {
      nickNames: [...this.state.friends.nickNames, nickName]
    }
  })

State should be only modified through the setState function because if you modify it directly you could break the React component lyfecycle.

Answer:4

I have setup ionic push for Android and IOS, the android version works great, but the IOS doesn't. I have setup the provisioning and certificate files on IOS and they seem to be fine. Also, the ...

I have setup ionic push for Android and IOS, the android version works great, but the IOS doesn't. I have setup the provisioning and certificate files on IOS and they seem to be fine. Also, the ...

  1. debug push notifications ios
  2. debug push notifications android
  3. debug push notifications
  4. chrome debug push notifications
  5. debug production push notifications
  6. xcode debug push notification
  7. debug apns push-notification
  8. debug apple push notification service
  9. send debug push notification
  10. ios debug production push notifications
  11. ionic debug push notification

Currently, I am facing one issue related to angularjs directive. I want to send outlet object from directive1 to directive2. Both directives having same controller scope. I tried with emitting event ...

Currently, I am facing one issue related to angularjs directive. I want to send outlet object from directive1 to directive2. Both directives having same controller scope. I tried with emitting event ...

  1. share data between components angular
  2. share data between docker containers
  3. share data between google sheets
  4. share data between processes
  5. share data between switch and switch lite
  6. share data between threads java
  7. share data between sibling components angular
  8. share data between iphone and ipad
  9. share data between fragments
  10. share data between components react
  11. share data between processes python
  12. share data between threads python
  13. share data between components vue
  14. share data between apps ios
  15. share data between iphones
  16. share data between threads c++
  17. share data between classes c++
  18. share data between controllers angularjs
  19. share data between android phones
  20. share data between apps android

In the following code, the $compile is not working. I have an item, I am adding this item to scope, so that I can compile this and get an html. $http({ method: 'GET', url :...

In the following code, the $compile is not working. I have an item, I am adding this item to scope, so that I can compile this and get an html. $http({ method: 'GET', url :...

  1. angularjs $compile not working
  2. angularjs works compile
  3. angularjs directive compile not working
  4. angularjs compile ng-click not working

I created a new vue project with the CLI and want to deploy it. Based on this documentation https://router.vuejs.org/guide/essentials/history-mode.html#html5-history-mode I added the history mode to ...

I created a new vue project with the CLI and want to deploy it. Based on this documentation https://router.vuejs.org/guide/essentials/history-mode.html#html5-history-mode I added the history mode to ...