简体   繁体   English

如何在React中将值传递给状态更改回调?

[英]How do you pass a value to a state change callback in React?

I have the following anonymous function callback after setState: 我在setState之后有以下匿名函数回调:

  processOnBlur(event) {
      this.setState({
        input: event.target.id
      }, (event) => {
        console.log(event.target.id);
      });
  }

However, event inside the callback is undefined. 但是,回调中的event未定义。 How can I pass the event object to that callback? 如何将事件对象传递给该回调? Is it even possible? 可能吗

Passing a function callback to setState() will execute that function after the state change has occurred, which in this case will mean that even correctly passed in, your event target will be null inside of the callback (a state change happened and your component re-rendered, React wiped the event and you no longer have a target). 将函数回调传递给setState()将在状态更改发生后执行该函数,在这种情况下,这意味着即使正确传递,您的事件目标在回调内部也将为null(状态更改发生了,并且组件重新-rendered,React清除了事件,您不再有目标)。

There are a few ways you can solve this, first off, you did just set the state of input to be equal to the event.target.id meaning you have access to it in your callback function through state: 有几种方法可以解决此问题,首先,您只是将输入的状态设置为等于event.target.id这意味着您可以通过状态在回调函数中访问它:

processOnBlur(event) {
  this.setState({
    input: event.target.id
  }, () => {
    console.log(this.state.input);
  });
}

Another option would be to set a ref on the input, which is a React feature that gives you a reference to that component which you can freely use inside of your class functions. 另一个选择是在输入上设置ref ,这是一种React功能,可为您提供对该组件的引用,您可以在类函数中自由使用该组件。

So on your input: 因此,在您的输入上:

<input id="username" 
       type="text" 
       ref="myInput"
       onBlur={(e) => this.processOnBlur(e)} />  

Then you can access it in your event handler through this.refs.myInput , like so: 然后,您可以通过this.refs.myInput在事件处理程序中访问它,如下所示:

processOnBlur(event) {
  this.setState({
    input: event.target.id
  }, () => {
    console.log(this.refs.myInput.id);
  });
}

// You could also use it to set your input state if you really wanted to, 
// though event.target is probably more appropriate.
//
// input: this.refs.myInput.id

EDIT: An extra method I forgot (and maybe the simplest in this situation) is to call event.persist() on the event that was passed to your event handler: 编辑:我忘记了(可能是这种情况下最简单的方法)的另一种方法是在传递给事件处理程序的事件上调用event.persist()

processOnBlur(event) {
  event.persit();

  this.setState({
    input: event.target.id
  }, () => {
    console.log(event.target.id);
  });
}

This prevents React from wiping the event reference so you can retain access to it. 这可以防止React抹掉事件引用,因此您可以保留对其的访问。

You're covering up your event object with a new variable that's also called event - the stuff in the parentheses of a arrow function are its parameters, not what you're passing in. This is what your code should look like: 您正在用一个也称为event的新变量覆盖event对象-箭头函数括号中的内容是其参数,而不是您要传递的参数。这是代码应如下所示:

processOnBlur(event) {
    this.setState({
        input: event.target.id
    }, () => {
        console.log(event.target.id);
    });
}

Whereas effectively before, you were doing: 鉴于之前有效,您正在做:

function callback(event) { // This is a different event parameter!
    console.log(event.target.id);
}

processOnBlur(event) {
    this.setState({
        input: event.target.id
    }, callback); // See? You're not actually passing event in
}

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

相关问题 如何使用 React Hooks 将 state 值传递给回调 function - How to pass state value to a callback function using React Hooks 如何从不同的组件更改反应组件的 state? - How do you change state of a react component from a different component? 如何在点击事件中更改React的状态? - How do you change the state in React on a click event? 如何将变量传递给回调函数? - How do you pass variable to callback function? 在React中调用组件时如何传递状态(值)? - How Do I Pass State (Value) when Calling Component in React? 如何正确地将回调和 state 传递给 React Router Dom 中的 Layout 元素? - How to correctly pass a callback and a state to the Layout element in React Router Dom? 在 React 中,如何将“this.state”作为参数传递给“socket.on”回调 function? - In React, how to pass “this.state” as parameter to “socket.on” callback function? 如何将递增的值传递给回调 - how do I pass an incrementing value to a callback React - 如何使用 handleInputChange function 来更改 state 值内的 state 值? - React - How do I use a handleInputChange function to change a state value that is within a state value? 如何使用react-router将顶级组件状态传递给路由? - How do you pass top level component state down to Routes using react-router?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM