reactjs - 将父道具异步更新到子组件
问题描述
如何在异步操作后将更新的父组件的 props 发送到子组件。有没有可用的生命周期方法
class SignUp extends Component {
constructor(props){
super(props);
this.state = {...props};
}
componentDidMount(){
this.props.UserSignupType();
}
render(){
<Inputs {...this.state} >
}
}
const stateToProps = state => ({
signupInputs: state.signupInputs
});
connect(stateToProps, null)(SignUp);
class Inputs extends Component {
constructor(props){
super(props);
this.state = {...props};
}
render(){
if(!this.state.isLoaded) {
return <Loading />
} else {
let inputs = [];
this.state.inputs.forEach(function(input){
inputs.push(<TextField value={input.text}>)
});
return <View>{inputs}</View>
}
}
}
注意:异步更新后传递道具可能重复给子组件
解决方案
第一次更正:如果您不更新它,则不需要从道具中添加状态数据。此外,如果您正在更新它,它应该处于子组件的状态。所以父组件可以像这样直接将 props 传递给子组件:
<Inputs {...this.props} >
现在在Inputs
组件中,props
如果它不会更改,您可以直接使用,或者您可以使用componentWillReceiveProps
函数将其添加到状态,如下所示:
componentWillReceiveProps(nextProps){
this.setState({...nextProps});
}
推荐阅读
- python - 如何在 TensorFlow 中的这个神经网络中实现剩余的“for循环”
- android - 在画布上绘图时屏蔽圆形/形状
- css - Django CSS 相对路径在 Amazon S3 中不起作用
- django - djangocms:将数据从已弃用的 cmsplugin-filer-*-plugins 移动到 djangocms-*-plugins
- java - 如何在 JMETER 中将环境属性 json 文件读取到 UDV
- hive - Hive 插入权限被拒绝:user=root,access=WRITE
- validation - Magento 2如何在keyup事件上验证客户注册表单
- python - SQL比较两个选择子句和分组总和
- json - Neo4j 弹簧 JSON
- node.js - 节点请求返回空正文