React Native State未更新数组
我试图通过更新初始状态将数据加载到数组中,但是状态不会更新,并且返回原始的空状态。
以下是状态初始化,使用效果和箭头功能以更新状态的快照。
const [state, setState] = useState({
show: false,
isFocused: false,
......
rewardsCards: [] // this is the state that needs to get updated.
})
useEffect(() => {
getRewardsCards
checkPermissionsAsync()
getMembersCards()
props.navigation.addListener('willFocus', payload => {
getMembersCards()
})
}, [state.refreshing])
const getRewardsCards = async () => {
console.log('getRewardsCards running')
let rewardscards = await membershipProviderService.getMembershipProvider()
console.log('rewardscards', JSON.stringify(rewardscards, null, 2))
setState({
...state,
rewardsCards: rewardscards
})
console.log('rewardscards set', state.rewardsCards)
}
const ....
任何帮助将不胜感激。提前致谢!
I'm trying to load data into an array by updating the initial state of it, but the state doesn't get updated and it return the original empty state.
Below is snapshot of the state initialisation, use effect and the arrow function to update the state.
const [state, setState] = useState({
show: false,
isFocused: false,
......
rewardsCards: [] // this is the state that needs to get updated.
})
useEffect(() => {
getRewardsCards
checkPermissionsAsync()
getMembersCards()
props.navigation.addListener('willFocus', payload => {
getMembersCards()
})
}, [state.refreshing])
const getRewardsCards = async () => {
console.log('getRewardsCards running')
let rewardscards = await membershipProviderService.getMembershipProvider()
console.log('rewardscards', JSON.stringify(rewardscards, null, 2))
setState({
...state,
rewardsCards: rewardscards
})
console.log('rewardscards set', state.rewardsCards)
}
const ....
Any help will be greatly appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论