reactjs - 异步更新后将 props 传递给子组件
问题描述
异步操作后更新的 props 必须传递给 Inputs 组件。异步操作完成后如何将 props 传递给子组件。
我正在尝试在子组件中不连接。这可能吗
class SignUp extends Component {
componentDidMount(){
this.props.UserSignupType();
}
render(){
<Inputs {...this.props} >
}
}
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>
}
}
}
@Adam 回答后更新:
注意:我已经尝试过 this.state.isLoaded 和 this.props.isLoaded。使用这两种方法我无法获取数据
更新 2:
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>
}
}
}
解决方案
你在正确的轨道上。
您不需要将您的子组件连接到 redux 商店。您正在将道具传递给孩子,但您的问题是在孩子的构造函数中,您将这些道具放入状态然后丢弃它们。因此,对父级道具的任何后续更改都不会传播给子级。
在您的孩子中,您应该这样做if(!this.props.isLoaded)
而不是if(!this.state.isLoaded)
. 这应该可以解决您的问题。
推荐阅读
- javascript - 在 Node js 中创建计时器,每 10 分钟重置一次,并在点击 api 时返回当前分钟
- python - 如何从字符串中提取数字?
- firebase - 如何从通过 Firebase 上传到 Cloud Storage 的文件中删除 EXIF 数据
- eclipse - 组合两个数据集时,BIRT 查看器中的行跳过
- javascript - SyntaxError Unexpected token < in JSON at react.js API 导航中的位置 0
- react-native - 在应用程序安装后使用带有 react-native 的分支引用识别发件人
- bash - bash 中的 for 循环有问题吗?
- ssl - 移动应用程序中的公钥固定与证书固定
- javascript - 自动大写文本框光标自动跳转到结尾
- javascript - 在 React 中将 Blob 转换为 Cell 内的图像