【发布时间】:2017-12-01 07:56:36
【问题描述】:
对不起,如果这听起来像是一个重复的问题,我已经检查了现有的答案,但似乎没有一个能解决我的问题。
我最初设置一个受控输入的值,例如 value ={this.props.someValue}(来自 API)
稍后,我试图让用户在表单上键入值,
class ProfilePage extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
lastname: "",
errors : ""
};
this.handleOnchange = this.handleOnchange.bind(this);
}
handleInputChange = event => {
const { target } = event;
this.setState({
[target.name]: target.value,
errors: errors
});
};
handleOnchange(event) {
const { target } = event;
this.setState({
[target.name]: target.value
});
}
render(){
let firstName = [];
let lastName = [];
if (this.props.profile) {
firstName = this.props.profile.first_name;
lastName = this.props.profile.last_name;
}
return (
<div class="container">
<label for="first-name">First Name</label>
<input
type="text"
placeholder="First Name"
name="name"
value={firstName.value}
onBlur={this.handleInputChange}
onChange={this.handleOnchange.bind(this)}
className={
errors && errors.name
? "inputError"
: "inputValid"
}
/>
</div>
)
}
}
我的 onChange 事件成功触发,但它不允许我在输入框中输入任何内容。我错过了什么?
【问题讨论】:
标签: javascript reactjs react-redux