Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - Possible Unhandled Promise Rejection (id: 0): undefined is not an object ('prevComponentInstance._currentElement')

In react native I am trying to get json data using axios.get(my_url_path), then I'm update my state with response.data to 'urldatabase' key ,if I trying to call this state key and read the data from the json that I got I get the error:

Possible Unhandled Promise Rejection (id: 0): undefined is not an object ('prevComponentInstance._currentElement').... 

my code is as follow:

  }
  constructor(props) {
    super(props);
    this.state = { userAnswer: '', count: 0, urldatabase: {} };
  }



  componentWillMount(){
    axios.get('my_url_path')
      .then(response => this.setState({urldatabase: response.data}));
  }
  render() {
    let num = this.state.count;
    console.log(this.state.urldatabase[num].book)
    const book = this.state.urldatabase[num].book
     return(
        <RiddleHeader headerText={book} />
)
}
}

Can anyone please explain why am I getting this error? what I done wrong if any?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider: In componentWillMount, what happens if the axios promise is rejected? Right: It's unhandled. You need a catch handler in there. The basic rule of promises: Always handle rejection or return the promise to the caller. In this case, since React won't do anything with it if you return the promise out of componentWillMount, you need to handle the error within componentWillMount.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...