6

I'm new to reactjs and i'm learning to change state in reactjs. I'm trying to initialize an array object in state with unknown length, and later update the array object using set state. Following is the code:

state = {
  error: null,
  temperature: null,
  isLoaded: false,
  Weather5days: [],
};

So I have initialized Weather5days as an array, with unknown length. I get the data for Weather5days through API as json, so after fetch, I do this:

then(json => {
  var newArr = json.Forecasts.map(function(val) {
    //Forecasts have length of 5 array
    return {
      date: val.Date,
    };
  });

  console.log(newArr); // I checked, data is copied correctly in newArr.

  this.setState({
    Weather5days: newArr, //set state of the weather5days
  });
});

Now above code works with no error, but when I call console.log(this.state.Weather5days), the array is empty. So I thought the way of initializing Weather5days was wrong so I tried the following:

state = {
  error: null,
  temperature: null,
  isLoaded: false,
  Weather5days: [{}, {}, {}, {}, {}], //array object of length 5
};

and It works. This is half the solution because in certain circumstances, you don't know the length of the array(that comes from the API) and I cannot afford to do literally do [{},{}..] everytime. What is the proper way of initializing array object with unknown length in state?

3
  • use the push method in array in order to put the value in the array, kinda like this. this.state.Weather5days.push(newArr); Commented Aug 22, 2018 at 5:04
  • @JohnWick if i directly push to Weather5days that would be directly mutating the state which should be avoided in reactjs as far as I know Commented Aug 22, 2018 at 5:06
  • You can update your state like this this.setState({ Weather5days: [...this.state.Weather5days, newArr] }) or like this: this.setState({ Weather5days: this.state.Weather5days.concat([newArr]) }) Commented Aug 22, 2018 at 5:15

4 Answers 4

2

Hi your defined the Weather5days as empty array and adding the newArr to it but you should take the previous state of array and add your newArr to the state like this

this.setState({
Weather5days : [...this.state.Weather5days, newArr]
})
Sign up to request clarification or add additional context in comments.

Comments

1

you can do it like this

const updated = this.state.Weather5days.slice(); 
updated.push(newArr); 
this.setState({Weather5days:updated}); 

1 Comment

no luck :( nothing at the moment works without initializing Weather5days:[{},{},{},{},{}] at the moment..keeps on returning empty array
1

hope this could work.please check

const {Weather5days}=this.state
              this.setState({
          Weather5days: [...Weather5days,newArr]
      });

1 Comment

no luck :( nothing at the moment works without initializing Weather5days:[{},{},{},{},{}] at the moment..keeps on returning empty array
-1

When you set array in state you need to verify its updated state in callback. Because of the behavior of states in react are asynchronous.

 this.setState({
    Weather5days: newArr  //set state of the weather5days
  },()=> {
     console.log(this.state.Weather5days); 
  });

I think you are missing this point only.

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.