2017-10-07 56 views
0

我有kidsDetail组件它有它自己的状态,在父组件childDetail组件中使用for循环呈现多个时间。我想要点击保存按钮,获取详细的kidsDetail信息列表。在父组件中获取子状态组件值

贝娄父组件代码

 renderKidsDetails(totalkids) { 
     /*If user selects 'yes' for kids, then this function is called 'n' 
     times; n = no. of kids */ 
     const kids= []; 
     if (this.state.addKidsDetails === true) { 
     for (let i = 0; i < totalkids; i++) { 
     kids.push(
     <RenderChildDetails key={i} ivalue={i + 1} /> 
    ); 
     } 

    } 
    return (
     <View style={{ marginLeft: 20 }}> 
     {kids} 
     </View> 
    ); 
     } 

KidsDetail.js它有两个状态的孩子年龄和性别等都需要这个值获得检索父组件

class RenderChildDetails extends Component { 
state={ 
    kidsAge: 0, 
    kidsGender: '', 
} 
constructor(props) { 
    super(props); 
    onClicked=this.onClicked; 
//Call function when the back button is pressed in the page header. 
    } 

onClicked=()=>{ 
console.log(" kid Age "+this.state.kidsAge); 
} 
render() 
    { 
    return(
    <View style={{flexDirection:'row',flex:1}}> 
    <View style={{backgroundColor:'black',height:20,width:20,borderRadius:10,margin:5,alignItems:'center', padding: 1}}> 
    <Text style={{color:'white'}}>{this.props.ivalue}</Text> 
</View> 
<TouchableOpacity onPress={() =>{ 

         ActionSheet.show(
          { 
          options: AGE, 
          cancelButtonIndex: CANCEL_INDEX, 
          destructiveButtonIndex: DESTRUCTIVE_INDEX, 
          title: "Select Age" 
          }, 
          buttonIndex => { 
          this.setState({ kidsAge: AGE[buttonIndex] }); 
          } 
         ) 

         } 
         }> 

    <View style={{flex:1,margin:5,flexDirection:'row'}} > 
    <Text>Age: {this.state.kidsAge}</Text> 
    <Image source={require('../../../Assets/Icons/Settings/Black_down.png')} 
      style={{height:11,width:11,margin:5}} /> 
    </View> 
    </TouchableOpacity> 
    <TouchableOpacity onPress={() => 
     ActionSheet.show(
         { 
          options: GENDER, 
          cancelButtonIndex: CANCEL_INDEX, 
          destructiveButtonIndex: DESTRUCTIVE_INDEX, 
          title: "Gender" 
         }, 
         buttonIndex => { 
             this.setState({ kidsGender: GENDER[buttonIndex] }); 
             } 
        )}> 
    <View style={{flex:1,margin:5,flexDirection:'row'}}> 
    <Text>Gender: {this.state.kidsGender}</Text> 
    <Image source={require('../../../Assets/Icons/Settings/Black_down.png')} 
      style={{height:11,width:11,margin:5}} /> 
    </View> 
    </TouchableOpacity> 
</View> 


); 

} 
} 

export default RenderChildDetails; 
+0

请格式化你的代码。 – topher

回答

0

在作出反应,你可以简单地传递一个处理程序将在子组件状态更新时调用子组件,然后将子组件状态存储在父组件状态中。

检查下面的代码:

class Parent extends Component { 
    constructor(props) { 
     super(props) 
     this.state = {}; 
     this.handleChildUpdate = this.handleChildUpdate.bind(this); 
     this.getChildState = this.getChildState.bind(this); 
    } 


    //handler to update current parent state when child state is updated 
    handleChildUpdate(updatedState){ 
     this.setState({ 
     childState:updatedState 
     }); 
    } 

    //log updated child state on button click 
    getChildState(){ 
     console.log(this.state.childState.age); 
    } 

    render(){ 
     <View > 
     <Child handleUpdate={this.handleChildUpdate} /> 
     <Button onPress={this.getChildState}> 
     </View> 
    } 
} 


class Child extends Component { 
    constructor(props) { 
     super(props) 
     this.updateAge = this.updateAge.bind(this); 
     this.state = { 
      age:"10" 
     }; 
    } 


    updateAge(text){ 
     this.setState({ 
     age:text 
     } ,() => { 
     //this handler is passed as a prop from parent component 
     //this is how we update the parent state when any change happens to child state 
     this.props.handleUpdate(this.state); 
     }); 
    } 


    render(){ 
     <View > 
     <TextInput onChange={(text) => { this.updateAge(text) } } /> 
     </View> 
    } 
} 
相关问题