【发布时间】:2018-06-04 20:59:39
【问题描述】:
我想从我的减少操作文件中导航。 第一个屏幕是登录,所以点击登录按钮后会调用一个 redux action creator 和 reducer 生成一个新状态。所以,在操作之后我想重定向到我的仪表板。
我正在使用
react native + redux + react 导航
登录组件文件(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@gmail.com"
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.js):
const RouterComponent = StackNavigator({
login: { screen: LoginCmp },
dashboard: { screen: DashboardCmp },
});
动作创建者文件(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.
};
【问题讨论】:
-
您必须订阅 redux 状态才能登录并在 redux 操作更改状态值时更改导航。这将是一个正确的方法。
标签: react-native redux react-redux jsx react-navigation