确保
super()在构造函数中首先调用。
你应该设置
this的
setAuthorState方法
class ManageAuthorPage extends Component { state = { author: { id: '', firstName: '', lastName: '' } }; constructor(props) { super(props); this.handleAuthorChange = this.handleAuthorChange.bind(this); } handleAuthorChange(event) { let {name: fieldName, value} = event.target; this.setState({ [fieldName]: value }); }; render() { return ( <AuthorForm author={this.state.author} onChange={this.handleAuthorChange} /> ); }}另一种选择基于
arrow function:
class ManageAuthorPage extends Component { state = { author: { id: '', firstName: '', lastName: '' } }; handleAuthorChange = (event) => { const {name: fieldName, value} = event.target; this.setState({ [fieldName]: value }); }; render() { return ( <AuthorForm author={this.state.author} onChange={this.handleAuthorChange} /> ); }}


