I started using react-native few weeks ago. For my first app, I used navigator for the navigation with its navigationbar component to display title and left/right hand side buttons. After reading facebook has dropped its support for the navigator and developing navigation experimental or navigation-rfc (will call 'NavExp' to make it short), I am trying to use the NavExp. But I can't get my head around it.
App menu: DrawerLayout for android, TabIOS for IOS. And will have navbar to display the title and right(content-specific menu - print, copy)/left(for menu) button depending on the content.
That's a whole lot to unpack in one SO question, so you're probably going to be better served by doing some research and then splitting this question into several smaller questions. Here's some pointers to get you started.
Overall: The purpose of the new NavigationExperimental is to create a stateless navigation structure for React Native, following the same principles as React. The old Navigator component was more reliant on maintaining and mutating state than the new NavExp. If you think about how React likes to take a set of props and then render out a full new UI whenever something changes, the new NavExp is meant to facilitate that a little more.
This because even more useful when you use a Flux-like pattern for managing state in your application. I would suggest reading up on Flux, or in my opinion, the more easy to grasp Redux implementation of the pattern.
That answers to an extent #1, and you'll better understand the answer to #2 after going through those links.
NavigationRootContainer is a helpful element (though not necessary) that provides some of the structure and functionality when using NavExp. The examples from Facebook utilize it. If you are implementing NavExp with something like Redux, you do not need to use one because you would be duplicating the usage of reducers.
I'm assuming you are talking about the states when deciding to render out the appropriate scene/card/screen? These can be declared anywhere, and really are just strings. You don't even need to declare them anywhere.
State is the collection of data and variables that make up the moving parts of your application. For example, if you had a shopping cart app you would store the customer's name and the contents of their cart in your application's state, as well as what screen they were currently on, what screen they had previously been on, etc. Anything that can change goes in state.
Actions are like shooting a flare into the sky to alert other parts of your application that something has changed. User adds a new item to their cart? Send an ITEM_ADDED_TO_CART
action, along with the ID of the item. User presses a button to the main screen? Send a NAVIGATE_TO_SCREEN
action, along with an identifier for the home screen. Actions get processed by reducers, and reducers make changes to the state and then tell React Native to start re-rendering everything.
renderScene
method with NavExp as well, which functions in a nearly identical way: it spits out the contents of the screen, whatever it should be.(FYI, I don't have any official word on this, but if you are already comfortable with Navigator and have implemented it, you are probably fine continuing with it for the foreseeable future, instead of rewriting your app to take advantage of NavigationExperimental.)
Based on the comments from here you should use NavigationExpermiental: https://github.com/facebook/react-native/issues/6184
Here's a nice example to get you started: https://github.com/thebakeryio/react-native-complex-nav
import { View, NavigationExperimental } from 'react-native';
import React, { Component } from 'react';
import styles from './styles';
import { connect } from 'react-redux';
import ApplicationTabs from '../ApplicationTabs';
import NewItem from '../NewItem';
const { CardStack: NavigationCardStack } = NavigationExperimental;
class GlobalNavigation extends Component {
render() {
return (
<NavigationCardStack
direction={'vertical'}
navigationState={this.props.navigation}
onNavigate={this.props.onNavigate}
renderScene={this._renderScene.bind(this)}
renderOverlay={this._renderHeader.bind(this)}
style={styles.main}
/>
);
}
_renderHeader(props) {
return null;
}
_renderScene(props) {
if (props.scene.navigationState.key === 'applicationTabs') {
return (
<View style={{flex: 1}}>
<ApplicationTabs />
</View>
);
}
if (props.scene.navigationState.key === 'new') {
return (
<View style={{flex: 1}}>
<NewItem onClose={this._onCloseNewItem.bind(this)} />
</View>
);
}
}
_renderTitleComponent(props) {
return null;
}
_onCloseNewItem() {
this.props.onNavigate({
type: 'BackAction'
});
}
}
function mapDispatchToProps(dispatch) {
return {
dispatch
};
}
function mapStateToProps(state) {
return {
navigation: state.get('globalNavigation')
};
}
export default connect(mapStateToProps, mapDispatchToProps, (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, dispatchProps, stateProps, {
onNavigate: (action) => {
dispatchProps.dispatch(
Object.assign(action, {
scope: stateProps.navigation.key
})
);
}
});
})(GlobalNavigation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With