Reactjs Can';按下按钮后,无法清除输入字段 class CategoryBox扩展组件{ 建造师(道具){ 超级(道具); 此.state={ newCategoryTitle:“”, errorMsg:null, updateCategoryTitle:“”, }; this.handleCreateCategory=this.handleCreateCategory.bind(this); this.handleUpdateCategory=this.handleUpdateCategory.bind(this); } ... ... HandleUpdate类别(e){ e、 预防默认值(); 这是我的国家({ updateCategoryTitle:“” }); } render(){ 返回( //... {this.setState({updateCategoryTitle:e.target.value});}}/> 更新 //... } }

Reactjs Can';按下按钮后,无法清除输入字段 class CategoryBox扩展组件{ 建造师(道具){ 超级(道具); 此.state={ newCategoryTitle:“”, errorMsg:null, updateCategoryTitle:“”, }; this.handleCreateCategory=this.handleCreateCategory.bind(this); this.handleUpdateCategory=this.handleUpdateCategory.bind(this); } ... ... HandleUpdate类别(e){ e、 预防默认值(); 这是我的国家({ updateCategoryTitle:“” }); } render(){ 返回( //... {this.setState({updateCategoryTitle:e.target.value});}}/> 更新 //... } },reactjs,Reactjs,我想在按下按钮后清除输入文本字段 按下按钮后,它似乎进入“handleUpdateCategory(e)”。但它没有清除按钮上方的输入字段 如何清除输入文本字段?答案在注释中。 未清除,因为您从未将状态值绑定到。请按如下方式更改它 答案在注释中。 未清除,因为您从未将状态值绑定到。请按如下方式更改它 未清除,因为您从未将状态值绑定到您的。请这样更改它。未清除,因为您从未将状态值绑定到。请这样更改它。 class CategoryBox extends Component { construc

我想在按下按钮后清除输入文本字段

按下按钮后,它似乎进入“handleUpdateCategory(e)”。但它没有清除按钮上方的输入字段

如何清除输入文本字段?

答案在注释中。
未清除,因为您从未将状态值绑定到。请按如下方式更改它
答案在注释中。
未清除,因为您从未将状态值绑定到。请按如下方式更改它

未清除,因为您从未将状态值绑定到您的
。请这样更改它
。未清除,因为您从未将状态值绑定到
。请这样更改它
class CategoryBox extends Component {
  constructor(props) {
    super(props);
    this.state = {
      newCategoryTitle: '',
      errorMsg: null,
      updateCategoryTitle: '',
    };
    this.handleCreateCategory = this.handleCreateCategory.bind(this);
    this.handleUpdateCategory = this.handleUpdateCategory.bind(this);
  }
  ...
  ...
  handleUpdateCategory(e) {
    e.preventDefault();
    this.setState({
      updateCategoryTitle: ''
    });
  }
  render() {
    return (
    //...
    <form>
      <input
        type={'text'}
        className={styles.tabSmallField}
        placeholder={ category.name }
        onChange={(e) => { this.setState({ updateCategoryTitle: e.target.value }); }} />
      <button type="submit" onClick={this.handleUpdateCategory}>Update</button>
    </form>
    //...
  }
}
It didn't clear because you never bind the state value to your <input>. Change it like this <input value={this.state.updateCategoryTitle} ...other props... />