我有一个带有HOC的登录页面,我通过了必须成功登录后才能呈现的组件。
在这里,如果我检查isLoggedIn并重定向到sigin-in的渲染中,则会出现错误
err:永久违反:超过最大更新深度。当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时,可能会发生这种情况。 React限制了嵌套更新的数量,以防止无限循环
saga.js
try{//call api
//put login success
<Redirect to="/app"/>
}
index.js
const AuthProfile=requireAuth(App);
In reactdom
<Route render={(props)=><AuthProfile {...props}/>} path="/app"/>
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
import { bindActionCreators } from 'redux';
export default function (ComposedComponent) {
class Authenticate extends React.Component {
componentDidMount() {
console.log("12ra")
this._checkAndRedirect();
}
componentDidUpdate() {
this._checkAndRedirect();
}
_checkAndRedirect() {
const { isLoggedIn, redirect } = this.props;
if (!isLoggedIn) {
redirect();
}
}
render() {
console.log("28ra")
return (
<div>
{ this.props.isLoggedIn ? <ComposedComponent {...this.props} /> : null }
</div>
);
}
}
const mapStateToProps = (state) => {
return {
isLoggedIn:state.signInReducer.isLoggedIn
};
};
const mapDispatchToProps = dispatch => bindActionCreators({
redirect: () => push('/')
}, dispatch)
//Authenticate.propTypes = propTypes
return connect(
mapStateToProps,
mapDispatchToProps
)(Authenticate);
}
HOC组件是否正确或缺少某些内容?
在saga中重定向是一种好习惯吗?
有人可以让我知道成功后如何进入应用程序组件吗?请帮忙谢谢
更新
saga.js
yield put({type:LOGIN_SUCCESS,payload:data})
yield put(push('/app'))
index.js
我需要Page1,Page2的
<AuthRoute
exact
path="/"
component={Page1}
redirectTo="/login"
authenticated={this.props.isLoggegIn}
/>
<AuthRoute
exact
path="/"
component={Page2}
redirectTo="/login"
authenticated={this.props.isLoggegIn}
/>
答案 0 :(得分:4)
我将告诉您最好的和最简单的方法,您可以通过它在应用程序的任何部分(即内部redux和外部)进行重定向。
步骤:1 创建 history.js 文件。
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
步骤:2 导入到 App.js 文件中(或为应用创建路由器的位置)。
import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import * as ROUTES from '../../constants/Routes';
import history from './history';
const App = () => {
return (<Router history={history}>
<div className='App'>
<Switch>
<Route exact path={ROUTES.NOTIFICATIONS} component={Notification} />
<Route exacr path={ROUTES.USER_PROFILE} component={UserProfile} />
<Route exact path={ROUTES.SIGN_IN} component={SignInPage} />
<Route exact path={ROUTES.SIGN_UP} component={SignUpPage} />
<Route path='*' component={SignInPage} />
</Switch>
</div>
</Router>
);
};
export default App;
步骤:3 在Saga文件中,只需导入历史记录即可。
import { push } from 'react-router-redux';
import { Auth } from "../service/api/Auth";
import history from '../containers/App/history';
import * as ACTION from '../constants/ActionType';
import { put, takeLatest, call } from "redux-saga/effects";
import { userLoginSuccess, userLoginFailed } from "../actions/auth";
export function* userLogin(loginData) {
const payLoad = loginData.payload;
try {
const loginUserData = yield call(Auth.userLoginApiCall, payLoad);
yield put(userLoginSuccess(TOKEN));
history.push('/posts'); // Redirect to Post Page
} catch (err) {
yield put(push(userLoginFailed()))
}
}
export default function* userLoginRequest() {
yield takeLatest(ACTION.USER_LOGIN_REQUEST, userLogin);
}
答案 1 :(得分:1)
以下是您可以从传奇中导航的方式:
saga.js
import { push } from 'react-router-redux';
...
// after API call & validate user
yield put(push('/app'));
index.js
<Route strict exact path='/app' component={App}>
<AuthRoute
exact
path="/"
component={Yourcomponent}
redirectTo="/login"
authenticated={hasAccessToken()}
/>