[英]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.