[英]How to call a function in react-redux
I am new to react-redux. 我是react-redux的新手。 Now I am working on a react-redux project. 现在我正在研究react-redux项目。 I want to call a function when my input changes in input tag. 我想在输入标签中输入更改时调用函数。 For this I apply "onchange" event in this like 为此我在这里应用“onchange”事件
<input {...this.props} onchange="this.callHandler(this.value)"/>
onchange event handler call a function "callHandler" which is define as onchange事件处理程序调用函数“callHandler”,定义为
callHandler = (value) => {
console.log("value in input",value);
}
I don't know why this function is not called. 我不知道为什么不调用这个函数。
My full code for this component is : 我对此组件的完整代码是:
import React from 'react';
type PropsType = {
};
export default class InputComponent extends React.Component<void, PropsType, void> {
callHandler = (value) => {
console.log("value in input",value);
}
render() {
console.log("InputComponent props",this.props);
const inputStyle = this.props.inputStyle;
return (
<input {...this.props} onchange="this.callHandler(this.value)"/>
);
}
}
I also don't know why we use {...this.props}. 我也不知道为什么我们使用{... this.props}。
Thanks in advance. 提前致谢。
The prop is onChange
instead of onchange
. 道具是onChange
而不是onchange
。 onChange
expects a function, not a string onChange
需要一个函数,而不是一个字符串
onChange={this.callHandler}
this.callHandler = event => {
console.log('value', event.target.value)
}
callHandler
gets passed an event, you can get the value of the event's target by doing event.target.value
, as above. callHandler
传递一个事件,你可以通过执行event.target.value
来获取事件目标的值,如上所述。
{...this.props}
means that all props from the component are passed to the input element, see spread attributes for further reading. {...this.props}
表示组件中的所有道具都传递给input元素,请参阅spread属性以便进一步阅读。
For example, 例如,
<InputComponent type="text" placeholder="foobar" />
Passes all props of InputComponent
(type and placeholder in this case) to the input
element, which can be useful when creating generic/smarter containers. 将InputComponent
所有道具(在本例中为type和placeholder)传递给input
元素,这在创建通用/更智能容器时非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.