目录javascript
React-Navigation是目前React-Native官方推荐的导航组件,代替了原用的Navigator。最近开始接触,作个笔记java
基础使用主要包括两部分react
组件引入后,能够经过提供的api createStackNavigator来建立路由,每一个路由元素都是一个对象api
import { createStackNavigator } from 'react-navigation'; export default createStackNavigator({ Home: { screen: App }, Demos: { screen: demo }, DebugList: DebugList, DebugDetail: DebugDetail })
在每一个具体的页面中,能够经过设置navigationOptions对象来对header进行必定程度的自定义网络
static navigationOptions={ headerTintColor:'#000', headerTitle: ( <Text style={{ flex: 1, textAlign: 'center', color: '#222222', fontSize: px2dp(18)}}>调试demo</Text> ), headerRight: <View/> }; --or-- static navigationOptions = ({ navigation, screenProps }) => { return { headerTintColor:'#000', headerTitle: ( <Text style={{ flex: 1, textAlign: 'center', color: '#222222', fontSize: px2dp(18) }}>网络日志</Text> ), // 这里之因此要判断 是由于在生命周期最开始的时候 这个params咱们还没给他绑定点击事件 headerRight: <View><Text onPress={navigation.state.params?navigation.state.params.navigatePress:null}>清空</Text></View> } }
能够经过对象或者函数两种形式进行定义,函数定义时自带两个参数navigation和screenProps。其中navigation主要是路由传参内容,screenProps主要是在定义router的时候带着的参数,一会再把navigationOptions的具体属性补充一下TODOapp
小白踩坑后知道navigationOptions中是不能直接访问reactComponent中的this对象的,所以也就不能直接和reactComponent进行通讯,这个时候怎么办呢?答案就是操做navigation对象,咱们能够经过在组件中从新定义navigation参数params的形式来处理,好比函数
static navigationOptions = ({ navigation, screenProps }) => { return { headerTintColor:'#000', headerTitle: ( <Text style={{ flex: 1, textAlign: 'center', color: '#222222', fontSize: px2dp(18) }}>网络日志</Text> ), // 这里之因此要判断 是由于在生命周期最开始的时候 这个params咱们还没给他绑定点击事件 headerRight: <View><Text onPress={navigation.state.params?navigation.state.params.navigatePress:null}>清空</Text></View> } } componentDidMount() { this.props.navigation.setParams({ navigatePress:this._clearStorage }) } _clearStorage = () => { global.storage.remove({ key:'netLog' }).then((logs) => { console.log('data removed') this.setState(previousState => { return { logList: [] } }) }) }
而在组件中去调用头部的内容时,也是主要去查询navigation这个对象中的state和params两个参数,先到这 上个厕所flex