【发布时间】:2020-11-14 09:24:33
【问题描述】:
constructor (props) {
super(props)
this.state = {
AyarAdi: '',
Bilgi: '',
FirmaKodu: '',
icerik: {
smsUser: '',
smsApi: '',
mesajAciklama: '',
password: ''
},
errors: {}
}
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
componentDidMount () {
this.props.AyarlarListesiAl(this.props.user.userCreds.FirmaKodu)
}
handleChange (event) {
this.setState({
icerik: {
...this.state.icerik,
[event.target.name]: [event.target.value]
}
})
}
render(){
<div>
<InputGroup className='mb-3'>
<FormControl
name='password'
placeholder='SMS Şifre'
onChange={this.handleChange}
value={
this.props.data.AyarlarListesi[0] !== undefined
? JSON.parse(this.props.data.AyarlarListesi[0].Bilgi).password
: this.state.icerik.password
}
style={{ maxWidth: 400, height: 40 }}
/>
<span style={{ color: 'red' }}>
{this.state.errors['password']}
</span>
</InputGroup>
</div>
}
const mapStateToProps = state => ({
user: state.user,
data: state.data
})
....
问题是,在componentDidMount中,我通过Redux action向服务器发送了一个get请求,它从redux reducer state中获取了我的数据; AyarlarListesi。我想将数据获取到输入元素值,因此当用户打开页面时,他/她可以更新以前的数据,只需在输入上更改它并单击提交。我可以成功地将数据放入输入,但我无法更改输入的值,它保持稳定,handleChange 函数不起作用。我怎样才能让它发挥作用?
【问题讨论】:
-
如果你的
AyarlarListesi数组在第 0 个索引上有元素,那么值是从 props 数据而不是state数据设置的,并且在onChange处理程序中,你正在更改状态数据。因此您的更改不会得到反映。 -
如何将数据传递给组件状态?在componentDidmount中,它没有立即执行,在渲染中,我可以将它传递给变量但不能通过setState传递给状态
标签: reactjs dom input redux server