react-native Navigation使用一

前端之家收集整理的这篇文章主要介绍了react-native Navigation使用一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本篇博客只是翻译记录一下

初始化项目

react-native init pro_name // pro_name是你自己的项目名称

然后需要加入navigation

npm install --save react-navigation

这时候运行react-native run-android可能会报错,说不认识run-android命令

就需要执行

yarn add react-navigation命令,然后再执行react-native run-android就好了。

上图,图不是很清晰,找来找去没找到好的录制gif的应用




首先是Index.js文件

  1. import { AppRegistry } from 'react-native';
  2. import App from './App';
  3.  
  4. AppRegistry.registerComponent('navigation_pro1',() => App);
然后是App.js文件
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6.  
  7. import React,{
  8. Component
  9. } from 'react';
  10. import {
  11. Platform,StyleSheet,Text,View
  12. } from 'react-native';
  13. import {
  14. StackNavigator
  15. } from 'react-navigation';
  16. import HomeScreen from './js/HomeScreen';
  17. import ChatScreen from './js/ChatScreen';
  18.  
  19.  
  20. const SimpleApp = StackNavigator({
  21. Home: {
  22. screen: HomeScreen
  23. },Chat: {
  24. screen: ChatScreen
  25. }
  26. });
  27.  
  28.  
  29. export default class App extends Component < {} > {
  30. render() {
  31. return <SimpleApp / > ;
  32. }
  33. }
  34.  
  35. const styles = StyleSheet.create({
  36. container: {
  37. flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},welcome: {
  38. fontSize: 20,textAlign: 'center',margin: 10,instructions: {
  39. textAlign: 'center',color: '#333333',marginBottom: 5,});
然后是HomeScreen.js文件
  1. import React,{ Component } from 'react';
  2. import {
  3. Platform,View,Button
  4. } from 'react-native';
  5. import {StackNavigator} from 'react-navigation';
  6.  
  7. export default class HomeScreen extends Component{
  8. static navigationOptions={
  9. title:'Welcome'
  10. };
  11. render(){
  12. const {navigate}=this.props.navigation;
  13. return (
  14. <View>
  15. <Text>Hello,Navigation!</Text>
  16. <Button
  17. onPress={()=> navigate('Chat',{user:
  18. '小明'})}
  19. title='Chat with Lucy'
  20. />
  21. </View>
  22. );
  23. }
  24.  
  25. }
最后是ChatScreen.js文件
  1. import React,{
  2. Component
  3. } from 'react';
  4. import {
  5. Text,View
  6. } from 'react-native';
  7. import {
  8. StackNavigator
  9. } from 'react-navigation';
  10.  
  11. export default class ChatScreen extends Component {
  12. static navigationOptions = ({
  13. navigation
  14. }) => ({
  15. title:`Chat with ${navigation.state.params.user}`
  16. });
  17. render() {
  18. const {
  19. params
  20. } = this.props.navigation.state;
  21. return ( < View >
  22. < Text > 我是ChatScreen页面 {
  23. params.user
  24. } < /Text>
  25.  
  26. < /View>
  27. );
  28. }
  29. }

一定要注意js的路径。

另外英语好的兄弟可以直接看文档

点击打开链接

猜你在找的React相关文章