Reactjs 异步更新后将道具传递给子组件
异步操作后更新的道具必须传递给输入组件。如何在异步操作完成后将道具传递给子组件 我正在尝试在子组件中不连接。这可能吗Reactjs 异步更新后将道具传递给子组件,reactjs,react-native,Reactjs,React Native,异步操作后更新的道具必须传递给输入组件。如何在异步操作完成后将道具传递给子组件 我正在尝试在子组件中不连接。这可能吗 class SignUp extends Component { componentDidMount(){ this.props.UserSignupType(); } render(){ <Inputs {...this.props} > } } const stateToProps = state => ({ signup
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>
}
}
}
类注册扩展组件{
componentDidMount(){
this.props.UserSignupType();
}
render(){
}
}
const stateToProps=状态=>({
signupInputs:state.signupInputs
});
连接(stateToProps,空)(注册);
类输入扩展组件{
建造师(道具){
超级(道具);
this.state={…props};
}
render(){
如果(!this.state.isLoaded){
返回
}否则{
让输入=[];
this.state.inputs.forEach(函数(输入){
push()的输入
});
返回{inputs}
}
}
}
@Adam-answer之后更新:
注意:我已尝试使用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>
}
}
}
类注册扩展组件{
建造师(道具){
超级(道具);
this.state={…props};
}
componentDidMount(){
this.props.UserSignupType();
}
render(){
}
}
const stateToProps=状态=>({
signupInputs:state.signupInputs
});
连接(stateToProps,空)(注册);
类输入扩展组件{
建造师(道具){
超级(道具);
this.state={…props};
}
render(){
如果(!this.state.isLoaded){
返回
}否则{
让输入=[];
this.state.inputs.forEach(函数(输入){
push()的输入
});
返回{inputs}
}
}
}
您走在正确的轨道上
您不需要将子组件连接到redux存储。您将道具传递给孩子,但您的问题是,在孩子的构造函数中,您将这些道具放入状态,然后丢弃它们。因此,对父级道具的任何后续更改都不会传播到子级
在你的孩子身上,你应该做
if(!this.props.isLoaded)
而不是if(!this.state.isLoaded)
。这应该可以解决您的问题。对不起,我需要编辑我的问题,我只使用道具。很抱歉@AdamYou仍然应该使用this.props而不是this.state。因为您的孩子不修改加载的状态变量,所以您可以从道具中进行检查。另外,您的父组件似乎没有将isLoaded
变量加载到它的props中,请检查您的mapstatetops
方法。数据在异步调用后在父props中更新,但在子props中不更新。如果您在子组件中使用props,我看不出有任何原因无法使用您提供的代码。也许你需要更新你的代码片段,但你不应该将你的道具存储在状态中,而应该像我之前建议的那样直接从中使用它们。道具
。你的有效谢谢,我正在更新我的问题。你能帮我吗