React Native State未更新数组

发布于 2025-01-17 23:23:45 字数 881 浏览 0 评论 0原文

我试图通过更新初始状态将数据加载到数组中,但是状态不会更新,并且返回原始的空状态。

以下是状态初始化,使用效果和箭头功能以更新状态的快照。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文