import React from "react";
class CommentBox extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ""
};
}
handlechange(event) {
this.setState({ value: event.target.value })
}
handlesubmit(event) {
alert(this.state.value)
event.preventDefault()
}
render() {
return (
<form className="p-5" onSubmit={() => this.handlesubmit()}>
<div className="formgroup">
<label>留言内容</label>
<input
type="text"
className="form-control"
placeholder="請輸入内容"
value={this.state.value}
onChange={() => this.handlechange()}
/>
</div>
<button type="submit" className="btn btn-primary">
留言
</button>
</form>
);
}
}
export default CommentBox;
onChange={(event) => this.handlechange(event)},这个才对,上面有问题
onChange={() => this.handlechange(event)},你的上个人也是这个问题
没有绑定到this实例, 可以这么写 onChange={ this.handlechange.bind(this)}
onChange={ this.handlechange()}