我知道以前有人问过这个问题,但我已经尝试了所有我能找到的解决方案,在浪费了几天之后,我真的要哭了。我究竟做错了什么?
尽管我越来越不顾一切地试图诱使输入字段表现得像最基本的文本输入字段,但输入字段仍然存在readonly并且onChange不会触发。
这是我的代码:
import React, { Component, PropTypes } from 'react';
export default class Contact extends Component {
constructor(props) {
super(props);
this.state = { name: '' };
}
handleChange(e) {
this.setState ({ name: e.target.value });
}
render() {
return (
<div>
<form>
<input
type = "text"
value = { this.state.name }
onChange = { this.handleChange.bind(this) }
/>
</form>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我刚刚意识到,只要我添加<Contact />到不调用componentDidMount(). 如果有人能够详细说明为什么会使输入字段变为只读,即调用是否componentDidMount()以某种方式干扰onChange或setState?
编辑 …