当前位置: 首页 > 知识库问答 >
问题:

调用setState更新程序时this.state当前吗?

汪坚
2023-03-14

我知道完全更新的状态会传递给setState的回调。不过,我想知道这个.state在当时是否也是最新的。原因是我有一个使用this.state的方法,我想知道从回调内部调用是否安全。例子:

areWeBigger = () => {
  const { theirSize } = this.props;
  const { ourSize } = this.state;

  return ourSize > theirSize;
};

attemptToEat = () => {
  this.setState(({ ourSize }) => {
    const bigger = this.areWeBigger(); // Is this safe?

    return { ourSize: bigger ? ourSize + 1 : ourSize - 1 }; 
  });
};

编辑

我现在意识到我所说的“回调”实际上被称为“更新程序”。很抱歉给你带来了困惑。

共有2个答案

田彬郁
2023-03-14

在您的案例中,似乎不需要setState回调。

从这里,您可以这样调用setState:

this.setState((prevState, props) => {
  return {counter: prevState.counter + props.step};
})

所以你可以像这样调用areWeBigger函数:

areWeBigger = (ourSize , theirSize ) => {
  return ourSize > theirSize;
};

attemptToEat = () => {
  this.setState(({ ourSize }, {theirSize }) => { //extract ourSize and theiSize from (current)state and props respectively
    const bigger = this.areWeBigger(ourSize, theirSize ); // this is before the state is updated
    return { ourSize: bigger ? ourSize + 1 : ourSize - 1 }; 
  });
};

在"areWeBigger"中使用直接状态访问来保持它:

areWeBigger = () => {
  const {ourSize} = this.state;
  const {theirSize} = this.props;
  return ourSize > theirSize;
};

callback = () => {
  // ... here the state is up to date
}

attemptToEat = () => {
  this.setState(({ ourSize }) => { //extract ourSize and theiSize from (current)state and props respectively
    const bigger = this.areWeBigger(); // this is before the state is updated
    return { ourSize: bigger ? ourSize + 1 : ourSize - 1 }; 
  }, callback); // to see the use of callback
};
殷安顺
2023-03-14

我认为应该行得通。语法也有点不同于您使用的语法。在上面的示例中,您没有使用回调,而是使用setState方法的功能约定。请参阅React JS文档以了解更多信息。此外,下面的代码片段可能会提供一种更好的方法来实现您想要实现的目标。

const areWeBigger = () => {
  const { theirSize } = this.props;
  const { ourSize } = this.state;
  return ourSize > theirSize;
};

const attemptToEat = () => {
  this.setState({ ourSize: "whatever value" }, () => {
    const bigger = this.areWeBigger(); // it is okay to use this
    // it doesn't return anything, that is not supported so remove your return statement
    // ideally you can also use componentDidUpdate lifecycle method if you don't want to always trigger a callback, you can refer react docs.
  })
     
  });
};
 类似资料:
  • 我有一个android应用程序,我正在使用FCM(Firebase Cloud Messaging)向我的用户发送推送通知,现在我有一个疑问,当用户更新我的应用程序时,FCM是否更新了应用程序的注册id。如果它更新了,那么如果我将向我的用户发送推送通知给他的旧register_id以及他的new_register id,那么他将收到两次或不收到相同的推送通知。 另外,如果他将收到两次推送通知,那么

  • 问题内容: 以上是我的bean配置,为什么在运行应用程序时出现错误。我的日志文件夹为空… 但是我已经将spring-web-mvc添加到确实包含此类文件的类路径中。 问题答案: 但是我已经将spring-web-mvc添加到确实包含此类文件的类路径中。 您如何向CLASSPATH添加内容? 如果要创建Web应用程序,则唯一合适的方法是将.class文件放在WEB-INF / classes下,并将

  • 在我的应用程序内,我想检查是否有任何更新的版本,我的应用程序是在app Store。如果有,那么必须通过警告消息通知用户,如果他/她选择升级,我想更新新版本。我想通过我的应用程序完成所有这些。这可能吗?

  • 问题内容: 是否有原因在循环中调用会阻止其多次更新状态? 我有一个非常基本的jsbin,突出了我所看到的问题。有两个按钮。一个将状态计数器更新为1。另一个在循环中调用One的基础函数- 似乎将多次更新状态。 我知道此问题的几种解决方案,但我想确保首先了解这里的基本机制。为什么不能循环调用?我是否笨拙地编写了代码,以至于无法达到预期的效果? 问题答案: 从React Docs: setState()

  • cmf_update_current_user($user) 功能 更新当前登录前台用户的信息 参数 $user: array 前台用户的信息 返回 无

  • X1.0新增 sp_update_current_user($user) 功能: 更新session里当前登录用户的信息 参数: $user:当前登录用户的最新信息 返回: 无