无法在 componentdidmount() 内的另一个屏幕中访问参数
Not able to access param in another screen inside componentdidmount()
state = {AnswerQuery : '', answerdetail: []};
getData() {
const data = this.props.navigation.getParam('data');
this.setState({
data
});
}
renderdata(){
console.log(this.state.data) // not able to recieve data here
const {answerdetail} = this.state
firebase.firestore().collection("queries").doc(this.state.data).collection("answers").onSnapshot((answerSnapShot) => {
console.log(this.state.data) // recieving data here
answerSnapShot.forEach((doc) => {
answerdetail.push(doc.data())
console.log(doc.data())
})
console.log(this.state.data) // recieving data here
this.setState(answerdetail)
// console.log(answerdetail)
//console.log(anotherentity)
})
}
componentDidMount(){
this.getData()
this.renderdata()
}
使用 getData() 函数从另一个屏幕接收值,但在将数据传递到 firebase firestore 命令时无法在 renderdata() 函数中使用该值,但我能够在 firebase firestore 命令中访问该数据.
this.state.data 在“doc”中传递时没有值 firebase firestore 函数 doc(this.state.data)
请帮忙
状态更新前调用renderdata
函数。
您可以更新您的 getData
以在状态更新后调用 renderdata
。
getData() {
const data = this.props.navigation.getParam('data');
this.setState({
data
}, this.renderdata);
}
state = {AnswerQuery : '', answerdetail: []};
getData() {
const data = this.props.navigation.getParam('data');
this.setState({
data
});
}
renderdata(){
console.log(this.state.data) // not able to recieve data here
const {answerdetail} = this.state
firebase.firestore().collection("queries").doc(this.state.data).collection("answers").onSnapshot((answerSnapShot) => {
console.log(this.state.data) // recieving data here
answerSnapShot.forEach((doc) => {
answerdetail.push(doc.data())
console.log(doc.data())
})
console.log(this.state.data) // recieving data here
this.setState(answerdetail)
// console.log(answerdetail)
//console.log(anotherentity)
})
}
componentDidMount(){
this.getData()
this.renderdata()
}
使用 getData() 函数从另一个屏幕接收值,但在将数据传递到 firebase firestore 命令时无法在 renderdata() 函数中使用该值,但我能够在 firebase firestore 命令中访问该数据.
this.state.data 在“doc”中传递时没有值 firebase firestore 函数 doc(this.state.data)
请帮忙
状态更新前调用renderdata
函数。
您可以更新您的 getData
以在状态更新后调用 renderdata
。
getData() {
const data = this.props.navigation.getParam('data');
this.setState({
data
}, this.renderdata);
}