我試過的要對此凡在我行動的幾種方法成功異步動作之後是做調度(推):重定向與之反應路由器V3
import LoginService from '../../services/login-service';
export const ActionTypes = {
SET_LOGIN: 'SET_LOGIN',
}
export function getLogin(data, dispatch) {
LoginService.getLoginInfo(data).then((response) => {
const login = response.data;
dispatch({
type: ActionTypes.SET_LOGIN,
login
})
// .then((res) => {
// dispatch(push('/'));
// })
})
}
我甚至看到一些關於使用渲染屬性在路由器上作用於路由器。
現在我想根據我的狀態是否是真的還是假的使用renderIf,但我似乎無法獲得道具此頁面上成功:
import React from 'react';
import renderIf from 'render-if';
import { Redirect } from 'react-router-dom';
import LoginHeader from '../../components/header/login-header';
import LoginPage from './login-page';
const LoginHome = props => (
<div>
{console.log(props)}
{renderIf(props.loginReducer.status === false)(
<div>
<LoginHeader />
<LoginPage />}
</div>)}
{renderIf(props.loginReducer.status === true)(
<Redirect to="/" />,
)}
</div>
);
export default LoginHome;
這裏是loginPage:
import React from 'react';
import { form, FieldGroup, FormGroup, Checkbox, Button } from 'react-bootstrap';
import { Field, reduxForm, formValueSelector } from 'redux-form';
import { connect } from 'react-redux';
import { getLogin } from './login-actions';
import LoginForm from './form';
import logoSrc from '../../assets/images/logo/K.png';
import './login.scss'
class LoginPage extends React.Component {
constructor(props) {
super(props);
this.state = {
passwordVisible: false,
}
this.handleSubmit= this.handleSubmit.bind(this);
this.togglePasswordVisibility= this.togglePasswordVisibility.bind(this);
}
togglePasswordVisibility() {
this.setState((prevState) => {
if (prevState.passwordVisible === false) {
return { passwordVisible: prevState.passwordVisible = true }
}
else if (prevState.passwordVisible === true) {
return { passwordVisible: prevState.passwordVisible = false }
}
})
}
handleSubmit(values) {
this.props.onSubmit(values)
}
render() {
return (
<div id="background">
<div className="form-wrapper">
<img
className="formLogo"
src={logoSrc}
alt="K"/>
<LoginForm onSubmit={this.handleSubmit} passwordVisible={this.state.passwordVisible}
togglePasswordVisibility={this.togglePasswordVisibility}/>
</div>
</div>
)
}
}
function mapStateToProps(state) {
return {
loginReducer: state.loginReducer,
};
}
function mapDispatchToProps(dispatch) {
return {
onSubmit: (data) => {
getLogin(data, dispatch);
},
};
}
export default connect (mapStateToProps, mapDispatchToProps)(LoginPage);
讓我知道,如果需要別的
我做了如下的變化,但在我的getLogin功能即使添加了第三個參數的歷史後,在我的第二個then'未定義''從'../../services/login-service'導入LoginService; 出口常量ActionTypes = { SET_LOGIN: 'SET_LOGIN', } 導出功能getLogin(數據,調度,歷史){ LoginService.getLoginInfo(數據)。然後((響應)=> { const的登錄名=響應。數據; 調度({ 類型:ActionTypes.SET_LOGIN, 登錄 }) }) 。然後((RES)=> { history.push( '/'); }) }''' –
可悲仍然undefined :(我會讓你知道,如果我弄明白了我沒有改變LoginHome也不包括renderIf –