【发布时间】:2019-12-15 01:06:07
【问题描述】:
从标题本身。我对反应还很陌生,想知道 您将如何将已经“传递”的数据从父级更新到子级。我有一个警报组件,它将根据 Axios 请求后获取的数据显示错误消息。
父母 ..
this.state = {
formContact: {
fullname: '',
contact:'',
email: '',
message: ''
},
formAlert: { alertMessage: 'default'}
};
handleClick() {
let rm = this;
axios({
method: 'post',
url: 'submit',
data: {
form: this.state.formContact
}
})
.then(function (response) {
let data = response.data.data;
rm.setState({
formAlert: { alertMessage: 'test' }
});
}).catch(function (response) {
//handle error
console.log(response);
});
}
render() {
return (
<div className="row">
<Alert data={this.state.formAlert} />
</div>
);
}
}
孩子
class Alert extends Component {
constructor(props) {
super(props);
console.log(props);
// Holds the form state and input boxes
this.state = {
formError: {
icon: '',
header: '',
message: '',
errorType: 'errormsg'
}
};
}
render() {
return (
<div className={'style-msg ' + this.state.formError.errorType}>
<div className="sb-msg"><i className="icon-thumbs-up"></i>
<strong>Well done!</strong>
{this.state.formError.message}
</div>
<button type="button" className="close" data-dismiss="alert" aria-hidden="true">×</button>
</div>
);
}
}
看来我无法将 formAlert.alertMessage 更新为“test”并将新数据“test”传递给孩子。
任何帮助将不胜感激。谢谢
【问题讨论】: