【问题标题】:Pass an update state React Native传递更新状态 React Native
【发布时间】:2020-04-24 08:56:04
【问题描述】:

所以我使用 set state 来更新我状态中的变量,并且在我想使用它之后的行中,但是它不使用新更新的状态,而是使用它之前的状态。

this.setState({filter: {startDate : new Date(new Date().setDate(new Date().getDate() - days))}})
this.props.fetchGraphEvents(this.props.currentUser, this.props.homeInfo, this.state)

在第二行this.state我想用之前刚刚更新了一行的新状态但是不行。

您会推荐任何 NPM 包或开源代码吗?

【问题讨论】:

    标签: react-native state jsx


    【解决方案1】:

    状态更新是异步的。

    你应该把第二行放在 setState 回调中

    例子:

    this.SetState({ title: 'New Title'}, () => {
        console.log(this.state.title) // new title is garantee
    });
    

    【讨论】:

    • 我这样做了,但它不起作用this.setState({filter: {startDate : new Date(new Date().setDate(new Date().getDate() - days))}}, () =>{ this.props.fetchGraphEvents(this.props.currentUser, this.props.homeInfo, this.state) })
    • 如果状态值已更新,您是否希望在回调中...问题出在其他地方,我的朋友
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 2020-09-06
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多