Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - Example of Navigation between views in React Native Android?

After check the React Native documentation, I still don't understand the best way to create views and navigate between different components.

I don't want to use any external components like:

https://github.com/Kureev/react-native-navbar/

https://github.com/23c/react-native-transparent-bar

https://github.com/superdami/react-native-custom-navigation

I don't need a Navigation bar, i just want to set views and slide left, right o pop a view, nothing more.

I know is something basic, but I can't find any helpful example.

Please, anyone can help me? Any functional example in https://rnplay.org/?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE 04/2018 :

Things have change since my first answer, and today two massive libraries are relevant for navigation : react-navigation and react-native-navigation.

I will wrote an example for react-navigation which is an easy to use library and full JS maintain by serious people from the community.

First install the library :

yarn add react-navigation

or

npm install --save react-navigation

Then in your app entry point (index.js) :

import Config from './config';

import { AppRegistry } from 'react-native';
import { StackNavigator } from 'react-navigation';

export const AppNavigator = StackNavigator(Config.navigation);
AppRegistry.registerComponent('appName', () => AppNavigator);

You can see that we pass an object to the StackNavigator, it's all ours screens configuration, here is an example of configuration :

import HomeScreen from "./HomeScreen";
import SettingsScreen from "./SettingsScreen";

const Config = {
      navigation: {
          Home: {
            screen: HomeScreen
          },
          Settings: {
            screen: SettingsScreen,
          }
       }
    }

Now the app know each screen we got. We can simply tell the "navigate" function to go to our Setting Screen. Let's watch our Home Screen :

class HomeScene extends Component {

      constructor(props) {
          super(props);
      }

      render () {
        return (
        <View>
            <Button
              title="Go to Settings"
              onPress={() => this.props.navigation.navigate('Settings')}
            />
        </View>

        );
      }
    }

As you can see, the navigation will hydrate the props. And from here you can make what you want.

Go to the library documentation to see all you can do : change the header type, the title, navigation type, ...

PREVIOUS ANSWER :

Watch this example: https://github.com/h87kg/NavigatorDemo

It's useful and a well written Navigator example, better than the one above you just wrote, I think.

Mainly watch the relationship between LoginPage.js and MainPage.js


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...