ReactJs - i can't catch a variable outside of the axios' loop

claudiobitar

I Have a JSON DB and a React Component that fetch these datas. Here is my JSON DB:

{
  "houses": [
    {
      "id": 1,
      "name": "house 1"       
    },
    {
      "id": 2,
      "name": "house 2"        
    },
    {
      "id": 3,
      "name": "house 3"
     }       
  ]
}

It is fetching by a ReactJS Component. When I do console.log() inside of a Axios' loop it works successfully. But inside of the render method it isn't working. How can I solve it?

class Houses extends Component {
  constructor(props) {
    super(props);
    this.state = {
      index:0,         
      currentHouse:[]      
    };      
  }

  componentDidMount() { 
    axios.get(URL_HOUSES)
      .then(res => {        
        this.setState({ index:0 })
        this.setState({ currentHouse: res.data})          
        //Here is working! It is returning the index and the current house's name      
        console.log('index:' + this.state.index)             
        console.log(
                     'Name of the current house:' +  
                      this.state.currentHouse[this.state.index].name
                    ) 
    }

  render() {                  

    return (
      <div>     
        //Here isn't working. It is returning an error message:  
        //TypeError: Cannot read property '0' of undefined

        <h3>{this.state.currentHouse[this.state.index].name}</h3>
      </div>
    );
  }
}   


export default Houses
lsimonetti

TL;DR check initial currentHouse array before displaying it in the render method.

On the initial render of your component, there are no elements in the currentHouse array.

So when your component tried to print out your statement this.state.currentHouse[this.state.index].name, what it's actually trying to do is find the 0th position of an empty array []. This will evaluate to undefined.

An option to fix this is set an initial value to the currentHouse array in the state, or check to see if there are values in the array. For example:

 <h3>{this.state.currentHouse.length && this.state.currentHouse[this.state.index].name}</h3>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I can't use a variable outside my loop (flutter)

Why I can redefine the same variable multiple times in a for loop but can't outside of a loop?

Can't print variable outside for-loop

For loop, how can i use loop variable outside loop

How i can set/change variable in catch error axios?

Can't compile lambda when I want to catch something outside

how can i get foreach loop variable outside of loop

how can i use for loop scope variable form outside for loop?

Why can't I define a count variable outside of my loop in this function (C++)

how can i use the i variable outside the for loop?

Can't acess variable outside forEach loop JavaScript

Why when I define the variable outside the loop, it doesn't work

Python: Why can't I call a method outside the if loop

How can I define a variable in loop render html reactjs

I can't use a variable outside of Wordpress shortcode function

How can I modify my code to print a variable outside, that was initialized inside a try-catch?

Reactjs - How can I loop through an API that has been chained using axios?

how can i use an inputed variable from user in a loop and outside the loop

I have a defined a variable inside a for loop, but when printing it outside of the loop it doesn't print correctly

REACTJS Module not found: Can't resolve 'axios'

How can I define a variable so it can be used outside a while loop in java?

Why a variable can be accessible outside the loop in Python?

I need to use variable, loop outside?

I can't use variable assigned in Try{} scope, in main scope, but after adding return in catch{} I can?

Can someone point out where the error is happening when I call this variable outside the loop?

Why can't I define a variable inside a for loop in this way?

Why can't I print a variable that is provided by user inside a loop?

Why can't I swap out the variable of the for loop with a random letter?

JavaScript - I can't save the value of a variable after a loop