React native,让我知道this.props.navigation.state

时间:2018-02-12 02:25:11

标签: javascript reactjs react-native

我正在尝试从A.class检索数据到B.claa。 当我在A级直接制作一个按钮时。 来自A的数据可以检索到B类中以显示。

然而,当我使用for循环创建按钮的功能时, 数据实际上不会被检索。它只是未定义..

这里有A班和B班。 所以基本上,在A级,有5个按钮链接到B级的屏幕 B屏幕应该从A获取数据以显示在其屏幕上。

请让我知道这个解决方案..

A类。

const renderButtons = (navigateG) => {
const views = [];
const Name= ["AB","BC","cd","ef","gg"]
for ( var i =0; i<5; i++){
views.push(
<Button
   key={i}
   onPress={() => navigateG('B', { 
                    Title:"hi",
                    Name:Name[i],

                })}
   title="Learn More"
   color="#841584"
   accessibilityLabel="Learn more about this purple button"
/>);
} // % buttons are created. 

return views;
}
export default class A extends Component {

 static navigationOptions = ({ navigation }) => ({
    title: 'A',
    headerLeft: <MenuButton navigation={navigation}/>
});

render() {  
  const { navigate } = this.props.navigation;    
    return ( 
     renderButtons(navigate) )
 }
     </ScrollView>

    );
 }
}

B班。

export default class B extends Component {
static navigationOptions = ({ navigation }) => ({
    title: `${navigation.state.params.Title}`
});
render(){return(   <Text>{ this.props.navigation.state.params.Name }</Text> )}

1 个答案:

答案 0 :(得分:0)

您可以使用this.props.navigation.state.params.Name而不是this.props.navigation.state.Name

来访问导航状态名称