react-navigationは、ユーザーがログインしてログインページに遷移するかどうかをどう判断しますか?
7893 ワード
本論文では、react-navigationが、ユーザーがログインしてログインしているかどうかを判定する方法を紹介します。
新規index.js
新規index.js
import React, {Component} from 'react';
import {AppRegistry, Text, View, Button,Image,StyleSheet,BackHandler,ToastAndroid} from 'react-native';
import { StackNavigator,TabNavigator,NavigationActions } from 'react-navigation';
//
import stroage from './StorageUtil';
import './Global'
import IndexScreen from './Index'
import MeScreen from './Me'
import Login from './Login'
//
const MainScreenNavigator = TabNavigator({
IndexScreen: {
screen: IndexScreen,
navigationOptions: {
tabBarLabel: ' ',
headerLeft:null,//
tabBarIcon: ({ tintColor }) => (
<Image
source={require('./img/ic_image.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
onNavigationStateChange:(()=> alert(" "))
// initialRouteName:'IndexScreen'
},
},
MeScreen: {
screen: MeScreen,
navigationOptions: {
tabBarLabel:' ',
tabBarIcon: ({ tintColor }) => (
<Image
source={require('./img/ic_me.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
// initialRouteName:'MeScreen'
}
}
},
{
// trueinitialRouteName:'MeScreen',//
// initialRouteName:'MeScreen',
lazy:true,// , , app , false, true
// order: ['IndexScreen','FindScreen','ListNewScreen','MeScreen'], //order tab ,
animationEnabled: false, //
tabBarPosition: 'bottom', // ,android
swipeEnabled: false, // tab
// backBehavior: 'none', // back Tab( ), none
tabBarOptions: {
activeTintColor: '#2196f3', //
inactiveTintColor: '#999', //
showIcon: true, // android icon, true
indicatorStyle: {
height: 0 // TabBar , 0
},
style: {
backgroundColor: '#fff', // TabBar
height: 60
},
labelStyle: {
fontSize: 14, //
marginTop:2
// lineHeight:44
},
}
});
//
const FirstApp = StackNavigator({
IndexScreen: {
screen: MainScreenNavigator,
// initialRouteName: 'IndexScreen'
},
MeScreen: {screen: MeScreen},
Login:{screen: Login},
}, {
initialRouteName: 'IndexScreen', //
navigationOptions: { // , static navigationOptions ( )
headerStyle:{elevation: 0,shadowOpacity: 0,height:48,backgroundColor:"#2196f3"},
headerTitleStyle:{color:'#fff',fontSize:16}, //alignSelf:'center'
headerBackTitleStyle:{color:'#fff',fontSize:12},
// headerTintColor:{},
gesturesEnabled:true,// ,iOS ,
},
mode: 'card', // , card( iOS push ), modal( iOS modal )
headerMode: 'screen', // , screen: , float: , none:
onTransitionStart: (Start)=>{console.log(' ');}, //
onTransitionEnd: ()=>{ console.log(' '); } //
});
//
const defaultGetStateForAction = FirstApp.router.getStateForAction;
FirstApp.router.getStateForAction = (action, state) => {
// MeScreen global.user.loginState = false || ''( )
if (action.routeName ==='MeScreen'&& !global.user.loginState) {
this.routes = [
...state.routes,
{key: 'id-'+Date.now(), routeName: 'Login', params: { name: 'name1'}},
];
return {
...state,
routes,
index: this.routes.length - 1,
};
}
return defaultGetStateForAction(action, state);
};
export default class FirstAppDemo extends Component {
render() {
return (
<FirstApp />
);
}
}
AppRegistry.registerComponent('FirstApp', () => FirstAppDemo);
const styles = StyleSheet.create({
icon: {
width: 26,
height: 26,
},
});
グローバルストアのStrageUtil.jsを新規作成します。
import React, {Component} from 'react';
import {AsyncStorage} from 'react-native';
import Storage from 'react-native-storage';
var storage = new Storage({
// , 1000
size: 1000,
// : RN AsyncStorage, web window.localStorage
// ,
storageBackend: AsyncStorage,
// , (1000 * 3600 * 24 ), null
defaultExpires: 1000 * 3600 * 24,
// 。 。
enableCache: true,
// storage , ,
// sync , 。
// sync
// sync
// , require
// , storage.sync
//sync: require('./sync') // sync
})
// ( )storage ,
// web
// window.storage = storage;
// react native
// global.storage = storage;
// , ** ** storage
// : ,
// storage
// global.storage = storage
//
global.storage = storage;
Global.js,
//
global.user = {
loginState:'',//
userData:'',//
};
//
storage.load({
key: 'loginState',
}).then(ret => {
global.user.loginState = true;
global.user.userData = ret;
}).catch(err => {
global.user.loginState = false;
global.user.userData = '';
})
登録コンポーネントLogin.js
_login() {//
// debugger;
ToastUtil.show(" ");
// key 。 , 。
// , , 。
storage.save({
key: 'loginState', // : key _ !
data: {
userid: '1001',
userName:'userName',
token: 'token'
},
// , defaultExpires
// null,
// 8
expires: 1000 * 3600 * 8
});
global.user.loginState = true;//
global.user.userData = { userid: '1001', userName:'userName', token: 'token'};//
setTimeout(()=>{
this.props.navigation.navigate('UserScreen')//
},2000)
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。