在反应组件中将箭头函数转换为 "ordinary" 函数表达式
Arrow function into "ordinary" function expression in react component
我想把这个react组件中的箭头样式函数(at onChange)改成普通函数。我是初学者,对我来说并排查看两个版本很有帮助,因为我很难掌握新的箭头函数语法。
这应该是可能的,但是使用 "ordinary" 函数,下面的 could 看起来如何?
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;
<div>
<input
onChange={function(event) {
props.handleChange("email", event.target.value)
}
}
/>
</div>
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;
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;
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>
)
}
}
我想把这个react组件中的箭头样式函数(at onChange)改成普通函数。我是初学者,对我来说并排查看两个版本很有帮助,因为我很难掌握新的箭头函数语法。
这应该是可能的,但是使用 "ordinary" 函数,下面的 could 看起来如何?
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;
<div>
<input
onChange={function(event) {
props.handleChange("email", event.target.value)
}
}
/>
</div>
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;
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;
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>
)
}
}