简体   繁体   English

ReactJS - 事件未定义,它不会呈现 onChange 事件?

[英]ReactJS - Events is undefined, It won't render onChange event?

在此处输入图像描述

I'm trying to create a converter from inches to cm and feet to cm but I want to have only one input and to switch between them with buttons.我正在尝试创建一个从英寸到厘米和从英尺到厘米的转换器,但我只想有一个输入并使用按钮在它们之间切换。 I created a value this.bol set to neutral and some click events to change that value.我创建了一个值this.bol设置为 neutral 和一些点击事件来更改该值。 Depending on that value the onChange event will listen and know which state to update but I get events is not defined.根据该值,onChange 事件将侦听并知道要更新哪个 state,但我得到的events未定义。

It is because you call the function without passing the event.这是因为您在没有通过事件的情况下调用了 function。 The callback function you pass to onChange gets the event as first parameter and you have to manually pass it to you custom handler.您传递给onChange的回调 function 将事件作为第一个参数获取,您必须手动将其传递给自定义处理程序。

<input onChange={(event) => {
  return this.handler(event);
}}>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM