我们可以重定向到 reduxSaga 吗

wit*_*low 6 reactjs redux redux-saga react-redux

我有一个带有 HOC 我传递组件的登录页面,必须在成功登录后呈现。

在这里,如果我检查 isLoggedIn 并在登录的渲染中重定向,那么我会收到错误

错误:不变违规:超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,就会发生这种情况。React 限制嵌套更新的数量以防止无限循环

传奇.js

           try{//call api
                //put login success
              <Redirect to="/app"/>        
            }
Run Code Online (Sandbox Code Playgroud)

索引.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);
          }       
Run Code Online (Sandbox Code Playgroud)

HOC 组件是正确的还是我遗漏了什么?

在 saga 中重定向是一种好习惯吗?

任何人都可以请让我知道成功后如何进入应用程序组件我被困在那里请帮助谢谢

更新

传奇.js

       yield put({type:LOGIN_SUCCESS,payload:data})
        yield put(push('/app'))
Run Code Online (Sandbox Code Playgroud)

索引.js

Ex for Page1,Page2 我需要

        <AuthRoute
      exact
      path="/"
      component={Page1}
      redirectTo="/login"
      authenticated={this.props.isLoggegIn}
    />

     <AuthRoute
      exact
      path="/"
      component={Page2}
      redirectTo="/login"
      authenticated={this.props.isLoggegIn}
    />
Run Code Online (Sandbox Code Playgroud)

iam*_*hah 6

以下是您可以从 saga 导航的方式:

传奇.js

import { push } from 'react-router-redux';
...
// after API call & validate user
yield put(push('/app'));
Run Code Online (Sandbox Code Playgroud)

索引.js

 <Route strict exact path='/app' component={App}>
 <AuthRoute
          exact
          path="/"
          component={Yourcomponent}
          redirectTo="/login"
          authenticated={hasAccessToken()}
        />
Run Code Online (Sandbox Code Playgroud)


Ina*_*man 5

我将告诉您最好和最简单的方法,您可以从中重定向应用程序的任何部分(即在 redux 内部和外部)。

步骤: 1 创建history.js文件。

import { createBrowserHistory } from 'history';

export default createBrowserHistory();
Run Code Online (Sandbox Code Playgroud)

第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;
Run Code Online (Sandbox Code Playgroud)

步骤: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);
}
Run Code Online (Sandbox Code Playgroud)