发布于 4年前

Ant Design Input 不能用setState了为什么??

问题描述

onchangeGoodR=(e)=>{
    console.log(e.target.value)
    this.setState({
       GoodRValue : e.target.value
    })
}
<Input 
    style={{flex:"1",marginLeft:"5px",}} 
    size="small" 
    defaultValue={item.classGoodRemind} 
    onChange={this.onchangeGoodR} 
    onFocus={this.onFocusGoodR} 
    onBlur={()=>this.onBlurGoodR(item)} 
/>

不知道什么原因! 这里的输入框 输不进东西! 只要把this.setState这段去掉就可以输入

同一个页面的Input就是这样子写的为什么这里就不能用了呢?

解决方案

defaultValue只是初始化显示的值

显然你想要input变成一个受控组件,那就必须给他赋值。

onchangeGoodR=(e)=>{
    console.log(e.target.value)
    this.setState({
       GoodRValue : e.target.value
    })
}

现在 GoodRValue 已经被赋值成功

你只不过是没给到回显, Input 上加上value={this.state.GoodRValue}

<Input 
    style={{flex:"1",marginLeft:"5px",}} 
    size="small" 
    value={this.state.GoodRValue}
    defaultValue={item.classGoodRemind} 
    onChange={this.onchangeGoodR} 
    onFocus={this.onFocusGoodR} 
    onBlur={()=>this.onBlurGoodR(item)} 
/>

就可以了!

©2020 edoou.com   京ICP备16001874号-3