Skip to content

Instantly share code, notes, and snippets.

@mspvirajpatel
Last active August 24, 2017 08:01
Show Gist options
  • Save mspvirajpatel/597c5e8cf66e86bae519a94dbcedb41c to your computer and use it in GitHub Desktop.
Save mspvirajpatel/597c5e8cf66e86bae519a94dbcedb41c to your computer and use it in GitHub Desktop.
Demo AppRouter
import { Router, Stack, Scene ,Reducer} from 'react-native-router-flux';
import LoginScreen from './LoginScreen'
import HomeView from './HomeView'
import {AsyncStorage} from "react-native";
const USER_KEY = "user-auth";
const reducerCreate = params => {
const defaultReducer = new Reducer(params);
return (state, action) => {
console.log("ACTION:", action);
return defaultReducer(state, action);
};
};
const getSceneStyle = () => ({
backgroundColor: "white",
shadowOpacity: 1,
shadowRadius: 3,
});
export default class App extends Component {
constructor(props, context) {
super(props, context);
this.state = {
logged: false,
loading: true,
};
};
componentWillMount(){
self = this;
isSignedIn().then(data => {
console.log('New Data' + JSON.parse(data));
JSON.parse(data) ? this.setState({
logged: true,
loading: false,
}) : this.setState({
loading: false,
})
});
};
render() {
return (
<Router createReducer={reducerCreate} tintColor="red" getSceneStyle={getSceneStyle}>
<Scene key="root" titleStyle={{alignSelf: "center"}} hideNavBar>
<Scene key="login" hideNavBar component={LoginScreen} initial={!this.state.logged} title="Login" onExit={()=>console.log("onExit")} type='reset' direction="vertical"/>
<Scene key="tabbar" initial={this.state.logged} type='reset' duration={0}>
<Scene key="main" tabs tabBarStyle={styles.tabBarStyle} tabBarSelectedItemStyle={styles.tabBarSelectedItemStyle} >
<Scene key="home" hideNavBar hideTabBar component={HomeView} title="Home" titleStyle={styles.titleStyle} initial onEnter={onNavigate}/>
</Scene>
</Scene>
</Scene>
</Router>
)
}
}
const isSignedIn = () => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(USER_KEY)
.then(res => {
if (res !== null) {
resolve(true);
} else {
resolve(false);
}
})
.catch(err => reject(err));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment