一、跳转详情页

首先新建一个详情页
配置
router.js index.js

  1. import HomePage from '../views/HomePage';
  2. import DetailPage from '../views/Details'
  3. const AppNavigator = createStackNavigator(
  4. {
  5. Home: {
  6. screen: HomePage,
  7. },
  8. Detail:{
  9. screen:DetailPage //配置
  10. }
  11. },
  12. {
  13. initialRouteName:"Home"
  14. }
  15. );

跳转

  1. <Button title="Go to Detail"
  2. onPress={() => this.props.navigation.navigate('Detail')}/>

二、跳转传参

跳转页面

  1. import React, { Component } from 'react';
  2. import { View, Text,Button } from 'react-native';
  3. export default class HomePage extends Component {
  4. render() {
  5. return (
  6. <View>
  7. <Text> HomePage </Text>
  8. <Button
  9. title="Go to Detail"
  10. onPress={this.toggleDetail}
  11. />
  12. </View>
  13. );
  14. }
  15. toggleDetail=()=>{
  16. this.props.navigation.navigate('Detail',{id:"1001"})
  17. }
  18. }

接收页面

  1. componentDidMount(){
  2. let id = this.props.navigation.getParam("id")
  3. Alert.alert(id)
  4. }

非Button按钮点击
加入TouchableOpacity 组件 写事件时一定要加指向

  1. import React, { Component } from 'react';
  2. import { View, Text,Image,StyleSheet,Dimensions,TouchableOpacity } from 'react-native';
  3. export default class HomePage extends Component {
  4. ...
  5. render() {
  6. return (
  7. <View style={styles.container}>{this.state.list.map(item=>{
  8. return(
  9. <View style={styles.wrap}>
  10. <TouchableOpacity onPress={()=>{this.toggleDetail(item.id)}}>
  11. <Image style={{width:100,height:100}} source={{uri:item.coverImgUrl}}/>
  12. <Text style={styles.name}>{item.name}</Text>
  13. </TouchableOpacity>
  14. </View>
  15. )
  16. })}
  17. </View>
  18. );
  19. }
  20. ...
  21. toggleDetail=(id)=>{
  22. console.log(id)
  23. this.props.navigation.navigate('Detail',{id:id})
  24. }
  25. }

接收是一样的方法