Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

antd 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就是这样子写的为什么这里就不能用了呢??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)} 
/>

就OK了!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...