How can I choose according value in AsyncStorage which screen should be displayed? I don't know why setting screen value 'Home' to InitialScreen variable doesn't work?
Once I log in login.js screen and I close app, after launching the app again I am navigated to login.js. But now I want to go to home.js screen.
Parent's file routes.js:
let InitialScreen
const RoutesNavigation = StackNavigator({
Login: { screen: Login },
Home: { screen: Home }
}, {
initialRouteName: InitialScreen,
navigationOptions: {
header: false,
}
});
export default class App extends Component {
constructor(props) {
super(props);
value = AsyncStorage.getItem('name');
if (value !== null) {
InitialScreen = 'Home'; //This doesn't change Initial screen!!!
console.log("JJJJJJJJJJJJJJJJJJ routes.js value !== null ");
}
}
render() {
return (
<RoutesNavigation />
);
}
}
This is login.js, where I store value from received json:
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
}
}
render() {
return (
<View style={styles.container}>
<TextInput
style={styles.textInput} placeholder='Username'
onChangeText={(username) => this.setState({ username })}
underlineColorAndroid='transparent'
/>
<TextInput
style={styles.textInput} placeholder='Password'
onChangeText={(password) => this.setState({ password })}
secureTextEntry={true}
underlineColorAndroid='transparent'
/>
<TouchableOpacity
style={styles.btn}
onPress={this.login}>
<Text>Log in</Text>
</TouchableOpacity>
</View>
);
}
login = () => {
var formData = new FormData();
formData.append('userName', this.state.username);
formData.append('password', this.state.password);
fetch('http://....', {
method: 'POST',
body: formData
})
.then((response) => response.json())
.then((responseJson) => {
console.log("JJJJJJJJJJJJJJJJJJJJJJJJJ name: " + responseJson.name);
AsyncStorage.setItem('name', responseJson.name);
this.props.navigation.navigate('Home');
})
.catch(() => {
console.log("JJJJJJJJJJJJJJJJJJ Wrong connection");
alert('Wrong connection');
})
}
}
This is home.js:
export default class Home extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.text}> Member area. You are logged in. </Text>
<TouchableOpacity
style={styles.btn}
onPress={this.logout}>
<Text>Log out</Text>
</TouchableOpacity>
</View>
);
}
logout = () => {
AsyncStorage.removeItem('name');
this.props.navigation.navigate('Login');
console.log("JJJJJJJJJJJJJJJJJJ Logged out");
}
}
Create your navigator in here:
value = AsyncStorage.getItem('name');
if (value !== null) {
InitialScreen = 'Home';
const RoutesNavigation = StackNavigator({
Login: { screen: Login },
Home: { screen: Home }
},{
initialRouteName: InitialScreen,
navigationOptions: {
header: false,
}
});
}
Because you are creating your navigator at the top with empty initial route but you are changing value in here so you must create here.
Hope it will work.
AsyncStorage is async.Because of the js nature thread won't wait result of this
AsyncStorage.getItem('name');
use callback with getItem
AsyncStorage.getItem('name',(error,result) => {
if (result!== null) {
//do something
}
});
Related
import React, { Component } from 'react'
import { Container, Content, Form, Item, Input, Label, Button,Text, Icon} from 'native-base'
import AsyncStorage from '#react-native-community/async-storage';
import authStore from '../store/authStore';
export default class Login extends Component {
constructor(props){
super(props);
this.state={
email:'',
password:''
}
}
handleLogin = async () =>{
let requestObject = {
email: this.state.email,
password: this.state.password
}
authStore.userLogin(requestObject, response => {
this.storeUserData(response.data.data);
this.props.navigation.navigate('Home');
})
}
storeUserData = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('#userData', jsonValue)
} catch (e) {
console.log(e);
}
}
render() {
return (
<Container>
<Content contentContainerStyle={{flex: 1, justifyContent:'center'}}>
<Form style={{margin:10}}>
<Item rounded last style={{margin:10}}>
<Icon active type="FontAwesome" name='user' />
<Input placeholder='User Name'
onChangeText={(email)=>this.setState({email})}
value={this.state.email}/>
</Item>
<Item rounded last style={{margin:10}}>
<Icon active type="FontAwesome" name='key' />
<Input placeholder='Password'
secureTextEntry
onChangeText={(password)=>this.setState({password})}
value={this.state.password}/>
</Item>
<Button rounded block style={{margin:10}} onPress={() => this.handleLogin()}>
<Text>Sign-In</Text>
</Button>
</Form>
</Content>
</Container>
)
}
}
const AuthStack = createStackNavigator();
AuthStackScreen = () =>
<AuthStack.Navigator>
<AuthStack.Screen name="Login" component={Login} />
</AuthStack.Navigator>
HomeStackScreen = () =>
<HomeStackDrawer.Navigator>
<HomeStackDrawer.Screen name="Home" component={HomeScreen}/>
<HomeStackDrawer.Screen name="Form" component={FormScreen}/>
<HomeStackDrawer.Screen name="Logout" component={Logout}/>
</HomeStackDrawer.Navigator>
export default class App extends Component{
constructor(props){
super(props);
this.state={
isloggedIn:false
}
this.loginStatusCheck();
}
loginStatusCheck = async () =>{
const userToken = await AsyncStorage.getItem('#accessToken');
if (userToken) {
this.setState({isloggedIn:true})
} else {
this.setState({isloggedIn:false})
}
}
render(){
return(
<NavigationContainer>
{this.state.isloggedIn ? <HomeStackScreen/> : <AuthStackScreen/>}
</NavigationContainer>
)
}
}
This is my App.js, I am checking if the user is logged in or not, then loading the Navigation stack accordingly. I know the problem, If I Logout, I want to navigate to the sign-in component, but this.props.navigation.navigate('Login') gives error. because I am not returning the Login route. How to solve this issue? Also, when I Log in same issue, as the Login is not present in the stack.
Thank you in advance
Included the login component
You will have to do some changes to fix this issue. Your problem is you are trying to access a screen in a navigation stack which is not there.
And the biggest problem is using a state variable in App.js to handle the switch of navigation stacks. You can resolve this by maintaining the login status in a context in your application. You can update it from other screens as well. Once you update the login status you dont have to worry about the navigation and your condition in the App.js will manage that for you.
The code should be something like below. I have given a sample Login component which will update the context. You will have to switch to functional component. From your code i dont see any problem of doing that.
const AppContext = createContext({
isloggedIn: {},
setLoggedIn: () => {},
});
const Login = () => {
const { setLoggedIn } = useContext(AppContext);
return (
<View>
<Button onPress={() => setLoggedIn(true)} />
</View>
);
};
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
isloggedIn: false,
loading: true,
};
this.loginStatusCheck();
}
setLoggedIn = value => {
this.setState({ isloggedIn: value });
};
loginStatusCheck = async () => {
const userToken = await AsyncStorage.getItem('#accessToken');
if (userToken) {
this.setState({ isloggedIn: true, loading: false });
} else {
this.setState({ isloggedIn: false, loading: false });
}
};
render() {
if (this.state.loading) return <ActivityIndicator />;
return (
<AppContext.Provider
value={{
isloggedIn: this.state.isloggedIn,
setLoggedIn: this.setLoggedIn,
}}>
<NavigationContainer>
{this.state.isloggedIn ? <HomeStackScreen /> : <AuthStackScreen />}
</NavigationContainer>
</AppContext.Provider>
);
}
}
Hope this helps.
I defined a custom view as a component for headerRight property in navigationOptions as bellow:
static navigationOptions = ({ navigation }) => {
return {
headerRight: navigation.getParam('headerRight', null),
};
};
and then in componentDidMount:
this.props.navigation.setParams({
headerRight: (<MessageDetailsHeader {...this.props}
title = {this.state.headerTitle}
subTitle = {this.state.headerSubTitle}
online = {this.state.online}
/>)
})
also i defined some state for updating values:
constructor(props) {
super(props);
this.state = {
headerTitle: null,
headerSubTitle: null,
headerOnline: false
};
}
Custom header view component defined as bellow:
export default class MessageDetailsHeader extends React.Component {
constructor(props) {
super(props);
this.state = {
title: this.props.title,
subTitle: this.props.subTitle,
online: this.props.online
};
}
componentWillReceiveProps(nextProps) {
this.setState({
title: nextProps.title,
subTitle: nextProps.subTitle,
online: nextProps.online,
})
}
render() {
return (
<View style={styles.headerContainer}>
<View style={styles.headerDetailsContainer}>
<Text style={styles.headerTitle}>{this.state.title}</Text>
<Text style={styles.headerSubTitle}>{this.state.subTitle}</Text>
</View>
<Avatar small rounded source={require('../images/no-profile.png')} activeOpacity={0.7} avatarStyle={this.state.online? styles.avatarOnline: styles.avatarOffline}/>
</View>
);
}
}
I need to call setState in screen and then update Custom View in navigation header, is this a right way?
Thanks in advance
Solved!
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerRight: <MessageDetailsHeader
title={params.headerTitle}
subTitle={params.headerSubTitle}
online={params.headerOnline}
/>
};
};
and then called bellow code to set new value, easily!
this.props.navigation.setParams({
headerSubTitle: 'online',
});
I am trying to logout the user and then send him to SignedOut screen but when I press on Sign Out it is calling the unauthUser function but then it restart the switch navigator to dashboard and I have to go in profile screen to tap again on Sign Out to go out. Any idea what am I doing wrong in all this?
Here is my button from Profile.js
<TouchableOpacity
onPress={() => onSignOut().then(() => {
this.props.dispatch(unauthUser)
navigation.navigate("SignedOut")
}
)}
>
<View style={styles.signOutButton}>
<Text style={styles.button}>SIGN OUT</Text>
</View>
</TouchableOpacity>
onSignOut() from Auth.js
export let USER_KEY = 'myKey';
export const onSignIn = async () => { await AsyncStorage.setItem(USER_KEY, 'true') };
export const onSignOut = async () => { await AsyncStorage.removeItem(USER_KEY) };
export const isSignedIn = () => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(USER_KEY)
.then(res => {
if (res !== null) {
// console.log('true')
resolve(true);
} else {
resolve(false);
// console.log('false')
}
})
.catch(err => reject(err));
});
};
unauthUser from authActions.js
exports.unauthUser = {
type: 'UNAUTH_USER'
}
and from authReducer.js
case 'UNAUTH_USER':
return {
user_id: undefined,
token: undefined
};
and here is my switch navigator
export const createRootNavigator = (signedIn = false) => {
return SwitchNavigator(
{
SignedIn: {
screen: SignedIn
},
SignedOut: {
screen: SignedOut
}
},
{
initialRouteName: signedIn ? "SignedIn" : "SignedOut"
}
);
};
class App extends Component {
constructor(props) {
super(props);
this.state = {
signedIn: false,
checkedSignIn: false
};
}
async componentWillMount() {
await isSignedIn()
.then(res => this.setState({ signedIn: res, checkedSignIn: true}))
.catch(err => alert("An error occurred"));
}
render() {
const { checkedSignIn, signedIn } = this.state;
// If we haven't checked AsyncStorage yet, don't render anything (better ways to do this)
if (!checkedSignIn) {
return null;
}
const Layout = createRootNavigator(signedIn);
return (
<SafeAreaView style={styles.safeArea}>
<View style={{flex: 1, backgroundColor: '#ffffff'}}>
<StatusBar barStyle="light-content"/>
<Layout />
<AlertContainer/>
</View>
</SafeAreaView>
)
}
};
I started React Native / Redux development for two weeks now, and i have some issues with the mapStateToProps and re-rendering.
Here are the facts :
mapStateToProps is called correctly with the right state (the new one)
render function is not called after the mapStateToProps
I have already check the troubleshooting page of Redux about that, and i dont have mutable issue
Here is the code :
Component
import React from 'react';
import { connect } from 'react-redux';
import { View, StyleSheet } from 'react-native';
import { Item, Input, Button, Text } from 'native-base';
import { signup } from './../actions/signup.action';
class Signup extends React.Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: '',
confirmPassword: ''
};
}
render() {
console.log('rendering', this.props);
return (
<View style={styles.container}>
<Item rounded>
<Input placeholder='Email' onChangeText={(text) => this.setState({email: text})} value={this.state.email} />
</Item>
<Item rounded>
<Input placeholder='Mot de passe' secureTextEntry={true} onChangeText={(text) => this.setState({password: text})} value={this.state.password} />
</Item>
<Item rounded>
<Input placeholder='Confirmation du mot de passe' secureTextEntry={true} onChangeText={(text) => this.setState({confirmPassword: text})} value={this.state.confirmPassword} />
</Item>
<Button rounded onPress={(e) => this.onSignup(this.state.email, this.state.password)}>
<Text>Créer un compte</Text>
</Button>
<Text>{this.props.isSignupLoading}</Text>
<Text>{this.props.signupError}</Text>
</View>
)
}
onSignup = (email, password) => {
this.props.signup(email, password);
}
}
const mapStateToProps = (state) => {
return {
isSignupLoading: state.isSignupLoading,
isSignupSuccess: state.isSignupSuccess,
signupError: state.signupError
};
};
const mapDispatchToProps = (dispatch) => {
return {
signup: (email, password) => dispatch(signup(email, password))
};
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
});
export default connect(mapStateToProps, mapDispatchToProps)(Signup);
Actions
import { firebaseAuth } from './../config/firebase';
export const signupLoading = (isSignupLoading) => {
return {
type: 'SIGNUP_LOADING',
isSignupLoading
};
}
export const signupSuccess = (isSignupSuccess) => {
return {
type: 'SIGNUP_SUCCESS',
isSignupSuccess
};
};
export const signupError = (error) => {
return {
type: 'SIGNUP_ERROR',
signupError: error.message
};
};
export const signup = (email, password) => {
return (dispatch) => {
dispatch(signupLoading(true));
firebaseAuth.createUserWithEmailAndPassword(email, password)
.then(() => dispatch(signupLoading(false)))
.then(() => {
console.log("signup.action#signup success");
dispatch(signupSuccess(true));
})
.catch((error) => {
console.log("signup.action#signup failed - " + error);
dispatch(signupLoading(false));
dispatch(signupError(error));
});
};
};
Reducer
const defaultState = {
isSignupLoading: false,
isSignupSuccess: false,
signupError: null
};
const signupReducer = (state = defaultState, action) => {
switch (action.type) {
case 'SIGNUP_LOADING':
return Object.assign({}, state, {
isSignupLoading: action.isSignupLoading
});
case 'SIGNUP_SUCCESS':
return Object.assign({}, state, {
isSignupSuccess: action.isSignupSuccess
});
case 'SIGNUP_ERROR':
return Object.assign({}, state, {
signupError: action.signupError
});
default:
return state;
}
}
export default signupReducer;
If you want more code about what i have done, ask me.
Thanks for your help.
Best regards.
I met the same issue as you. You should fix it by modifying your mapStateToProps as following. This is due to signupReducer/signupReducer/signupError is under signupReducer not state.
const mapStateToProps = (state) => {
return {
isSignupLoading: state.signupReducer.isSignupLoading,
isSignupSuccess: state.signupReducer.isSignupSuccess,
signupError: state.signupReducer.signupError
};
};
I'm new to react native and i'm trying to build a custom tab bar but i'm facing a problem when trying to display icons tab bar.
Here what i achieve so far.
Here my Custom TabBar component:
class TabBar extends Component {
renderItem = (route, index) => {
const {
navigation,
jumpToIndex,
} = this.props;
const isCapture = route.routeName === 'AddExpenses';
const focused = index === navigation.state.index;
const color = focused ? activeTintColor : inactiveTintColor;
if (isCapture === true) {
return (
<TouchableOpacity
key={route.key}
style={Styles.tab}
onPress={() => (navigation.navigate('AddExpensesModal'))}
>
<Ionicons
name={ioniconsByPlatform('add-circle')}
style={Styles.icon}
size={26}
/>
</TouchableOpacity>
);
}
return (
<TouchableWithoutFeedback
key={route.key}
style={Styles.tab}
onPress={() => (isCapture ? navigation.navigate('CaptureModal') : jumpToIndex(index))}
>
<View style={Styles.tab}>
<Text style={{ color }}>{route.routeName}</Text>
</View>
</TouchableWithoutFeedback>
);
}
render() {
const {
navigation,
} = this.props;
const {
routes,
} = navigation.state;
return (
<View style={Styles.tabBar}>
{routes && routes.map(this.renderItem)}
</View>
);
}
}
export default TabBar;
My Tab Navigator:
const MainTabNavigator = TabNavigator({
Summary: { screen: SummaryScreen },
AddExpenses: { screen: ExpensesScreen },
Expenses: { screen: ExpensesScreen },
}, {
tabBarComponent: TabBar,
});
export default MainTabNavigator;
And an example of a screen where i try to set my TabBarIcon:
const SummaryScreen = () => (
<View style={Styles.container}>
<Text>Summary</Text>
</View>
);
SummaryScreen.navigationOptions = {
title: 'Summary',
tabBarIcon: props => <TabBarIcon {...props} name="pulse" />,
};
export default SummaryScreen;
I want to be able to display my tab bar icons thanks to the navigationOptions property.
Do you have any idea how i can do this ?
If you feel TabNavigator is not powerful enough(which I think it's far from powerful), you could always customize a navigator view.
Here is my notes for customize a navigator view to replace TabNavigator:
export default class SectionTabView extends React.Component {
static propTypes = {
navigation: PropTypes.object
};
constructor(props) {
super(props);
}
render() {
const {router, navigation} = this.props;
const {routes, index} = navigation.state;
/**
* ActiveScreen is the current screen you see when you change you navigation state in tab bar
*/
const ActiveScreen = router.getComponentForState(navigation.state);
return (
<View style={Styles.section_container}>
<ActiveScreen
navigation={addNavigationHelpers({
...navigation,
state: routes[index],
})}
/>
<SectionTabBar navigation={navigation}/>
</View>
);
}
}
export default class SectionTabBar extends React.Component {
static propTypes = {
navigation: PropTypes.object
};
constructor(props) {
super(props);
}
getTabButtomGroupView() {
const {navigation} = this.props;
const {routes, index} = navigation.state;
let tabButtomGroupView = [];
routes.map((route) => {
let styles = [Styles.eventSection_tab];
const isClicked = routes[index].routeName === route.routeName;
if(isClicked){
styles.push(Styles.eventSection_tabClicked);
}
tabButtomGroupView.push(
<TouchableOpacity
onPress={() => {
/**
* when the routeName is equal to current routeName, we should stop navigate action
*/
if (routes[index].routeName === route.routeName) {
return;
}
navigation.navigate(route.routeName);
}}
style={styles}
key={route.routeName}>
<Text style={{color:'white'}}>{SectionRouteConfig[route.routeName].navigationOptions.title}</Text>
</TouchableOpacity>
)
});
return tabButtomGroupView;
}
render() {
return (
<View style={Styles.section_tabContainer}>
{this.getTabButtomGroupView()}
</View>
);
};
}
//SectionRouteConfig.js
export const sectionRouteConfig = {
XXX: {
screen: XXX, navigationOptions: {
title: XXX
}
},
XXX: {
screen: XXX, navigationOptions: {
title: XXX
}
}
};
export const SectionNavigator = createNavigator(TabRouter(sectionRouteConfig))(SectionTabView);
//Usage
render() {
const {dispatch, navigationState} = this.props;
return (
<SectionNavigator
navigation={
addNavigationHelpers({
dispatch: dispatch,
state: navigationState
})
}
/>
)
}
by the way I also use redux.
If those codes are too much for you , you can check the official example here:https://github.com/react-community/react-navigation/blob/master/examples/NavigationPlayground/js/CustomTabs.js