React Native Navigation

2019-08-01  本文已影响0人  北疆小兵

背景

在React Native中页面间跳转时需要用到react-navigation库,相关知识点如下

路由管理

使用createStackNavigator创建路由管理器,其中 'home'、'Details'为路由名称,这个名字可以随意定,screen为跳转页面的component名字, initialRouteName可以指定默初始加载的component名字


const RootStack = createStackNavigator(
  {
    Home: {
      screen: HomeScreen,
      title: "Home"
    },
    Details: {
      screen: DetailsScreen,
      title: "detail"
    }
  },
  {
    initialRouteName: "Home"
  }
);

const AppContainer = createAppContainer(RootStack);

export default class App2 extends React.Component {
  render() {
    return <AppContainer />;
  }
}

页面切换

onPress={() => {this.props.navigation.navigate("Details"); }}
onPress={() => this.props.navigation.goBack()}

参数传递

 this.props.navigation.navigate("Details",{itemId:10,otherParam: 'someDefaultValue'});
const {navigation} = this.props;
const itemId = navigation.getParam('itemId');
const otherParam = navigation.getParam('otherParam');
上一篇 下一篇

猜你喜欢

热点阅读