在 onChange 事件中调用 2 个函数 [英] Call 2 functions within onChange event

查看:38
本文介绍了在 onChange 事件中调用 2 个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组件有点卡住了,所以我需要从 props 调用 onChange

I'm a bit stuck with my component, I need to call onChange from props so

<input type="text" value={this.state.text} onChange={this.props.onChange} />

但也调用组件中的另一个函数 handleChange() 来更新 state.text,我试过了

but also call another function within the component called handleChange() that updates the state.text, I tried

 <input type="text" value={this.state.text} onChange={this.props.onChange; this.handleChange} />

但这似乎不起作用.

推荐答案

将两个函数放在双引号中即可:

You can do it by putting the two functions in double quotes:

<input type="text" value={this.state.text} onChange="this.props.onChange(); this.handleChange();" />

这应该有效.但是如果你在第一个函数中调用第二个函数会更好:

This should work. But it would be better if you call the second function within the first one:

function testFunction() {
    onChange();
    handleChange();
}

这篇关于在 onChange 事件中调用 2 个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆