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 an
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
I've found this example: https://rnplay.org/apps/HPy6UA
And I've written a tutorial about it: https://playcode.org/navigation-in-react-native/
It's really helpful to understand the Navigation in React Native:
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
TouchableOpacity,
} = React;
var SCREEN_WIDTH = require('Dimensions').get('window').width;
var BaseConfig = Navigator.SceneConfigs.FloatFromRight;
var CustomLeftToRightGesture = Object.assign({}, BaseConfig.gestures.pop, {
// Make it snap back really quickly after canceling pop
snapVelocity: 8,
// Make it so we can drag anywhere on the screen
edgeHitWidth: SCREEN_WIDTH,
});
var CustomSceneConfig = Object.assign({}, BaseConfig, {
// A very tighly wound spring will make this transition fast
springTension: 100,
springFriction: 1,
// Use our custom gesture defined above
gestures: {
pop: CustomLeftToRightGesture,
}
});
var PageOne = React.createClass({
_handlePress() {
this.props.navigator.push({id: 2,});
},
render() {
return (
<View style={[styles.container, {backgroundColor: 'green'}]}>
<Text style={styles.welcome}>Greetings!</Text>
<TouchableOpacity onPress={this._handlePress}>
<View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
<Text style={styles.welcome}>Go to page two</Text>
</View>
</TouchableOpacity>
</View>
)
},
});
var PageTwo = React.createClass({
_handlePress() {
this.props.navigator.pop();
},
render() {
return (
<View style={[styles.container, {backgroundColor: 'purple'}]}>
<Text style={styles.welcome}>This is page two!</Text>
<TouchableOpacity onPress={this._handlePress}>
<View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}>
<Text style={styles.welcome}>Go back</Text>
</View>
</TouchableOpacity>
</View>
)
},
});
var SampleApp = React.createClass({
_renderScene(route, navigator) {
if (route.id === 1) {
return <PageOne navigator={navigator} />
} else if (route.id === 2) {
return <PageTwo navigator={navigator} />
}
},
_configureScene(route) {
return CustomSceneConfig;
},
render() {
return (
<Navigator
initialRoute={{id: 1, }}
renderScene={this._renderScene}
configureScene={this._configureScene} />
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
color: 'white',
},
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
module.exports = SampleApp;
I suggest you use react-navigation you can take a look to the Navigator Playground example
Install navigation module.
npm install --save react-navigation
Import it in your app
import {
TabNavigator,
} from 'react-navigation';
const BasicApp = TabNavigator({
Main: {screen: MainScreen},
Setup: {screen: SetupScreen},
});
...and navigate
class MainScreen extends React.Component {
static navigationOptions = {
label: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to Setup Tab"
onPress={() => navigate('Setup')}
/>
);
}
}