React native 反应:无法在componentWillMount中设置状态
我是react native的业余爱好者,我似乎无法理解下面代码中发生的事情:我可以毫无问题地将承诺中的“数据”记录在控制台上,但当我尝试使用this.setState设置状态“喜欢”和“喜欢”时,它们似乎并没有从最初的状态更改。此函数在React native 反应:无法在componentWillMount中设置状态,react-native,React Native,我是react native的业余爱好者,我似乎无法理解下面代码中发生的事情:我可以毫无问题地将承诺中的“数据”记录在控制台上,但当我尝试使用this.setState设置状态“喜欢”和“喜欢”时,它们似乎并没有从最初的状态更改。此函数在组件willmount()内部调用。知道我做错了什么吗?任何提示都会大有帮助 getMatchesUid= (uid, idPet)=>{ Ofirebase.database().ref('relationships').child(uid).c
组件willmount()内部调用。知道我做错了什么吗?任何提示都会大有帮助
getMatchesUid= (uid, idPet)=>{
Ofirebase.database().ref('relationships').child(uid).child('pets').child(idPet).on('value', snap=>{
const relations = snap.val()
const allMatchesUid = this.getOverlap(relations.liked,relations.likedBack)
console.log('allMatches', allMatchesUid)
this.stateFunction(this.state.matches, allMatchesUid)
const promiseliked = allMatchesUid.map(profileUid =>{
firebase.database().ref('relationships').child(uid).child('pets').child(idPet).on('value', snap=>{
const matches = snap.val()
const liked = this.getValue(uid, profileUid, idPet, 'liked')
return (liked)
})
const promiselikedBack = allMatchesUid.map(profileUid=>{
const likedBack = this.getValue(uid, profileUid, idPet, 'likedBack')
return (likedBack)
})
Promise.all(promiseliked).then(data =>
this.setState({
liked: data,
}))
this.setState({
liked: data
}))
Promise.all(promiselikedBack).then(data => this.setState({
likedBack: data,
}))
首先,不要使用组件willmount()
来设置状态<在初始化(呈现)组件之前调用code>componentWillMount()
,因此无法在未初始化的组件上设置状态,请使用componentDidMount()
并设置状态,检查以下链接以了解有关此的详细信息
首先不要使用
组件willmount()
设置状态<在初始化(呈现)组件之前调用code>componentWillMount(),因此无法在未初始化的组件上设置状态,请使用componentDidMount()
并设置状态,检查以下链接以了解有关此的详细信息