简体   繁体   English

在反应中使用 dispatchEvent 时不会触发复选框更改事件

[英]Checkbox change event does not fire upon using dispatchEvent in react

I have a Checkbox component with an onChange event handler.我有一个带有onChange事件处理程序的Checkbox组件。 I want to trigger the onChange event in another elements click handler.我想在另一个元素click处理程序中触发onChange事件。 I am creating a custom event and dispatch the event in the click handler.我正在创建一个自定义事件并在单击处理程序中调度该事件。 I could see this works fine in a native way whereas I could not achieve the same in react.我可以看到这在本机方式下运行良好,而我无法在 react 中实现相同的效果。

Use of Checkbox component Checkbox组件的使用

<Checkbox onChange={this.handleChange} />

I have created an example, please find the URL https://stackblitz.com/edit/react-dvrssp?file=Checkbox.js我已经创建了一个示例,请找到 URL https://stackblitz.com/edit/react-dvrssp?file=Checkbox.js

Your help is highly appreciated.非常感谢您的帮助。

Couple of things.几件事。 You are not using the correct prop in your CheckBox component.您没有在 CheckBox 组件中使用正确的道具。 You are accessing this.props.onChange , it should be this.props.onCLick instead.您正在访问this.props.onChange ,它应该是this.props.onCLick

Also you need to make a call to the function on the radio button onChange .您还需要call onChange单选按钮上的函数。

I have edited the code.我已经编辑了代码。 You can check it here.你可以在这里检查。 https://stackblitz.com/edit/react-mwqfxv?file=Checkbox.js https://stackblitz.com/edit/react-mwqfxv?file=Checkbox.js

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

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