0
繼我的第一個React教程。我的代碼看起來和教程完全一樣,但我的輸入不會在表單組件中重置。我第一次提交時,一切正常,但國家堅持第一個價值。即使撥打console.log
在回撥中撥打setState
,似乎setState
甚至不會觸發。 this
綁定在我的constructor
函數中。React - setState不重置輸入
import React, { Component } from 'react';
import TenantActions from '../actions/TenantActions';
export default class AddTenantForm extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
}
this.onSubmit = this.onSubmit.bind(this);
}
onSubmit(event) {
event.preventDefault();
console.log('1. On Submit click, sending addNewTenant action w/', this.state);
TenantActions.addNewTenant(this.state);
this.setState = ({ name: '' });
}
render() {
return (
<form>
<div className="form-group">
<input type="text"
className="form-control"
id="tenantName"
placeholder="Bob Smithers"
value={this.state.name}
onChange={e => this.setState({ name: e.target.value })}
/>
</div>
<button className="btn btn-default"
onClick={this.onSubmit}
>Submit</button>
</form>
)
}
}
唉,一個錯字。謝謝! –