1

I have default state like this:

this.state = {
  location:{
    lat: 1234,
    lng: 3245
  }
}

so every time I want to update either lat or lng I have to do this

this.setState({ location: {...this.state.location, lat: newLate} })

or

this.setState({ location: {...this.state.location, lng: newLng } })

if I have several setState in my component I have to write a lot of ..., worse if I have one level of nested object to work with. Also it's dangerous to do this {this.state.location.lat} because it will have error and it make the whole app to crash if location is not defined. What's the tip when you work on multiple nested array of object or object in react?

6
  • multiple nested array - but ... there is no array Commented Jan 24, 2018 at 5:17
  • @JaromandaX I did mentioned or Commented Jan 24, 2018 at 5:17
  • the tip is that ... makes the code more compact already, how short do you want the code to be? Commented Jan 24, 2018 at 5:20
  • @JaromandaX I think there are some library like normlizr to solve this problem. Commented Jan 24, 2018 at 5:24
  • well, then use such a library Commented Jan 24, 2018 at 5:25

2 Answers 2

6

Whenever possible, keep your state as 'flat' as possible.

In your case, you can simply do:

this.state = {
  lat: 1234,
  lng: 3245
}

As your state becomes bigger, use naming to segregate the different properties

this.state = {
  locationLat: 1234,
  locationLng: 3245
}

Even in applications with hundreds of components, I never need to use nested states.

Additional remarks:

  • Split your component into smaller pieces whenever you see this pattern
  • Only use a nested object if you know the entire object will be updated each time

From your location object:

const location = {
    lat: 1234,
    lng: 3245
}

initialize your state like so:

this.state = location
Sign up to request clarification or add additional context in comments.

3 Comments

sometime you can't do whatever you want, I'm a frontend who doesn't have control over the structure of the response data, done by another team.
but you are free to transform the response into whatever object you want to use in your component.
edited my post. If thats not what you asked for, please update you question with more details on what your API response actually is or post a new question so as not to be too confusing. Thanks
1

It is always recommended to keep the state flat as possible. So In your case it can be

state={
  locationLat:123,
  locationLng: 456,
}

The main reason of it comes from How object.assign works. It copy the value for only first level.

See the native implementation of Object.assign to understand more. Since it copy only for first level it is recommended to keep state flat.

var a = {b:1, c:{d:1}}

var e = Object.assign({},a)

console.log(a===e) // It will false

e.c.d = 2

console.log(a.c.d) //It will be 2

Ref to Read more:

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.