我想将此React组件中的箭头样式函数(在onChange)更改为普通函数。我是一个初学者,对我来说,同时查看两个版本会很有帮助,因为我很难理解新箭头函数语法。
应该有可能,但是下面的“使用普通”功能会如何?
import React from "react";
function Input1(props) {
//console.log(props)
return (
<div>
<input
onChange={event => props.handleChange("email", event.target.value)}
/>
</div>
);
}
export default Input1;
答案 0 :(得分:5)
import React from "react";
function Input1(props) {
//console.log(props)
function onChange(event) {
props.handleChange("email", event.target.value)
}
return (
<div>
<input
onChange={onChange}
/>
</div>
);
}
export default Input1;
答案 1 :(得分:1)
<div>
<input
onChange={function(event) {
props.handleChange("email", event.target.value)
}
}
/>
</div>
答案 2 :(得分:1)
import React from "react";
function Input1(props) {
function changeHandler(event) {
props.handleChange("email", event.target.value)
}
//console.log(props)
return (
<div>
<input
onChange={changeHandler}
/>
</div>
);
}
export default Input1;
答案 3 :(得分:-1)
import React from "react";
class Input1 extends React.Component {
constructor(props) {
super(props);
//need to bind in case of using function decleration
this.handleChange = this.handleChange.bind(this);
}
handleChange(event){
props.handleChange("email", event.target.value);
}
render() {
return (
<div>
<input
onChange={this.handleChange}
/>
</div>
)
}
}