I want to navigate from my reduc action file. 1st screen is login so after click on login button will call a redux action creator and reducer generate a new state. So, after the action i want to rediret to my dashboard.
I am using
react native + redux + react navigation
Login component file (LoginCmp.js):
import React, { Component } from 'react';
import {
Text,
View,
ScrollView,
KeyboardAvoidingView
} from 'react-native';
import { connect } from 'react-redux';
import { fieldChange, LoginAction } from '../../actions';
import { Button, Section, Input, Alert } from '../Helpers';
LoginAction(){
const payload = {
email: this.props.email,
password: this.props.password
};
this.props.LoginAction(payload); // Action call
}
render() {
return (
<KeyboardAvoidingView
style={styles.container}
behavior="padding"
>
<View>
<ScrollView contentContainerStyle={styles.contentContainer}>
<Text style={styles.headerText}> Login </Text>
{this.alertMessage()}
<Section>
<Input
placeholder="[email protected]"
onChangeText={this.onFieldChange.bind(this, { actionType: 'EMAIL_CHANGED' })}
value={this.props.email}
/>
</Section>
<Section>
<Input
secureTextEntry
placeholder="Password"
onChangeText={this.onFieldChange.bind(this, { actionType: 'PASSWORD_CHANGED' })}
value={this.props.password}
/>
</Section>
<Section>
<Button
onPress={this.LoginAction.bind(this)}
style={styles.buttonLogin}
>
Submit
</Button>
</Section>
</ScrollView>
</View>
</KeyboardAvoidingView>
);
}
}
const mapStateToProps = ({ auth }) => {
console.log(auth);
return auth;
};
export default connect(mapStateToProps, { fieldChange, LoginAction })(LoginCmp);
Router file (Router.js):
const RouterComponent = StackNavigator({
login: { screen: LoginCmp },
dashboard: { screen: DashboardCmp },
});
Action creator file (AuthActions.js):
import firebase from 'firebase';
import { NavigationActions } from 'react-navigation';
// Login actions
export const LoginAction = (payload) => {
return (dispatch) => {
firebase.auth().signInWithEmailAndPassword(payload.email, payload.password)
.then(user => loginSuccess(dispatch, user))
.catch((error) => {
loginFail(dispatch, error);
});
};
};
// Login success function
logoutSuccess = (dispatch, user) => {
// dispatch an action
dispatch({
type: 'LOGOUT_SUCCESS',
payload: user
});
### From here i want to navigate to dashboard.
};