简体   繁体   English

如何从回调方法中获取 state 的更新值?

[英]How to get the updated value of a state from a callback method?

I am building a React app and I have a code following this logic:我正在构建一个 React 应用程序,并且我有一个遵循此逻辑的代码:

class ComponentTest extends Component {
  state = {test: 0}

  testingHandler = () => {
     console.log(this.state.test)
  }

  updateHandler = () => {
     let test = Math.random()  
     this.setState({test})
     this.testing()
  }
        
  render () {
     return (
        <button onClick={this.updateHandler}>Update</button>
     )
  }
}

What I need to do is get the updated value of test in testingHandler() , however it doesn't happen, testingHandler just get the past value, it is like the state that is being received by testingHandler is always one step late.我需要做的是在testingHandler()中获取test的更新值,但是它不会发生, testingHandler只是获取过去的值,就像 testingHandler 收到的testingHandler总是晚一步。

For example, let's say that test = 0 , then I click on the button that calls updateHandler() and now test = 1 , after that updateHandler() calls testingHandler() , which I expect to get the new value of test which now is 1 , however I get the old value 0 .例如,假设test = 0 ,然后我单击调用updateHandler()的按钮,现在单击test = 1 ,之后updateHandler()调用testingHandler() ,我希望得到test的新值,现在是1 ,但是我得到旧值0 If I call updateHandler() again and update test to 2 I'll get 1 in testingHandler() , which is the past value.如果我再次调用updateHandler()并将test更新为2 ,我将在testingHandler()中得到1 ,这是过去的值。

How can I get this value updated from testingHandler ?如何从testingHandler更新此值?

I did everything that I could imagine, but I couldn't get this done.我做了我能想象到的一切,但我做不到。 I believe it has something to do with the render logic of React , but I don't know exactly how this is working.我相信它与React的渲染逻辑有关,但我不知道它是如何工作的。

PS Passing the value as an argument is not an option. PS 将值作为参数传递不是一种选择。

setState is asynchronous . setState 是异步的。

React may batch multiple setState() calls into a single update for performance. React 可以将多个 setState() 调用批处理到单个更新中以提高性能。

Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.因为 this.props 和 this.state 可能会异步更新,所以您不应依赖它们的值来计算下一个 state。

You can pass a callback function as the second argument of setState that will run right after the state has updated.您可以将回调 function 作为setState的第二个参数传递,该参数将在 state 更新后立即运行。 So in you case, you could do this:所以在你的情况下,你可以这样做:

  updateHandler = () => {
     let test = Math.random()  
     this.setState( {test}, this.testingHandler )
  }

copied from react documentation link: https://reactjs.org/docs/faq-state.html从反应文档链接复制: https://reactjs.org/docs/faq-state.html

Calls to setState are asynchronous - don't rely on this.state to reflect the new value immediately after calling setState.对 setState 的调用是异步的 - 不要依赖 this.state 在调用 setState 后立即反映新值。 Pass an updater function instead of an object if you need to compute values based on the current state如果需要根据当前 state 计算值,请传递更新程序 function 而不是 object

better you can pass it as a callback function or you can also pass the value to the function.更好的是,您可以将其作为回调 function 传递,或者您也可以将值传递给 function。

This link is also helpful: setState doesn't update the state immediately此链接也很有帮助: setState 不会立即更新 state

  class ComponentTest extends Component {
    state = { test: 0 };

    testingHandler = () => {
      console.log(this.state.test);
    };

    updateHandler = () => {
      let test = Math.random();
      this.setState({ test }, () => this.testingHandler()); // pass it as a callback
     
    };

    render() {
      return <button onClick={this.updateHandler}>Update</button>;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 setState 回调中获取更新的状态? - How to get the updated state in setState callback? 无法在方法内更新 state 值 - can't get updated state value inside method 如何在方法中获取更新的状态? (反应和 Redux) - How to get updated state within a method? (React & Redux) 为什么在回调函数中反应状态(数组)为空? 为什么不使用来自外部范围的更新值? - Why is react state(array) empty inside callback function? Why is it not using the updated value from outer scope? 如何在 React useEffect 挂钩中获取更新的 state 值 - How to get updated state value in React useEffect hook 如何在交叉点观察者 React 中获取更新的 state 值? - How to get the updated state value in intersection observer React? 在套接字回调中未获得更新的状态值反应本机 - Not getting updated state value in socket callback react native redux state 在 InterSectionObserver 回调 function 中没有更新值 - redux state doesn't have updated value in InterSectionObserver callback function 如何在反应状态下存储回调函数的值? (谷歌地图) - How to store value from callback function in react state? (Google Maps) React Hooks-无法获取更新的状态值 - React Hooks - Unable to get updated state value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM