我如何使用this.props.navigation.navigate发送数组作为props?

时间:2018-07-04 07:54:29

标签: react-native react-navigation

我的数组是

     this.state={
          locations:[
              {name: 'kerala'},
              {name: 'maharashtra'},
              {name: 'tamilnadu'},
           ]} 

我的组件是

    <TouchableWithoutFeedback
        onPress={(e)=>this.props.navigation.navigate('MultiSelector',{
        locations:this.state.locations})}>
       <View><Text>select</Text></View>   
     </TouchableWithoutFeedback>
我如上所述发送道具。

和Multiselector组件就像

     render(){
        const { params} = this.props.navigation.state;
        const { navigation } = this.props;
        const locations = navigation.getParam('locations','kerala')
        return (
          <View style={styles.container}>
          {
            locations.map((item,index)=>{
              return(
             <View key={index}><Text>{item.name}</Text>
             </View>)
            })
           }
          </View>
        )

在multiselector中,我没有将locations数组作为props,它仅采用默认值(当我测试时)。并导致错误位置。地图不是功能。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<TouchableWithoutFeedback
    onPress={(e)=>this.props.navigation.navigate('MultiSelector',{...this.state.locations})}> //change your code to use ...
   <View><Text>select</Text></View>   
</TouchableWithoutFeedback>

其余的取决于您

答案 1 :(得分:0)

您可以使用以下方式访问它:

const locations = this.props.navigation.state.params.locations;

使用库StackNavigatorHelper

StackNavigator({
     Home: { screen: StackNavigatorHelper.paramsToProps(Home) },
     MultiSelector: { screen: StackNavigatorHelper.paramsToProps(MultiSelector) }
});

库链接:https://github.com/dayitv89/react-navigation-helper