【问题标题】:react-router not rendering specific routes (React + Redux)react-router 不渲染特定路由(React + Redux)
【发布时间】:2020-09-12 11:37:09
【问题描述】:

我正在将 React 与 Redux 一起使用。当经过身份验证时,即当 this.props.isAuthenticated=true 时 /add 没有被渲染,而不是 / 路由被渲染。但是,如果我将 /add 移出 this.props.isAuthenticated 条件,它将被渲染。我无法弄清楚为什么会这样。

App.js

import React, { Component } from 'react';
import { Route, Switch, withRouter, Redirect, Link} from 'react-router-dom';

import { connect } from 'react-redux';
import './App.css';
import asyncComponent from './hoc/asyncComponent/asyncComponent';
import * as action from './store/actions/index';

const asyncSignUp = asyncComponent(() => {
  return import('./containers/Signup/Signup');
})

const aysncLogin = asyncComponent(() => {
  return import('./containers/Login/Login');
})

const asyncAddStore = asyncComponent(() => {
  return import ('./containers/Add/Add');
})


const asyncGetHome = asyncComponent(() => {
  return import ('./containers/Home/Home');
})

class App extends Component {

  componentDidMount() {
    this.props.onTryAutoLogin();
  }

  render(){
    let route = (
      <Switch>
        <Route path="/signup" component={asyncSignUp} />
        <Route path="/login" component={aysncLogin} />

        <Redirect to="/"/>
       </Switch>
    );
    if(this.props.isAuthenticated){

        route = (
          <Switch>

            <Route path="/add" component={asyncAddStore} />

            <Route path="/" exact component={asyncGetHome}/>

            <Redirect to="/"/>
          </Switch>
        );


    }

    return (
      <div className="App">
        <header className="App-header">

          {route}

        </header>
      </div>
    );
  }
}

const mapStateToProps = state => {
  return {
    isAuthenticated: state.auth.token !== null,
    userData: state.auth.data
  };
};

const mapDispatchToProps = dispatch => {
  return {
    onTryAutoLogin: () => dispatch(action.authCheckState())
  };
};

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App));

authComponent.js

import React, { Component } from 'react';

const asyncComponent = (importComponent) => {
    return class extends Component {
        state = {
            component: null
        };

        componentDidMount(){
            importComponent().then(cmp => {
                this.setState({component: cmp.default});
            });
        }

        render(){
            const C = this.state.component;

            return C ? <C {...this.props}/> : null;
        }
    }
}

export default asyncComponent;

【问题讨论】:

    标签: reactjs react-redux react-router-dom


    【解决方案1】:

    你有&lt;Route path="/add" component={asyncAddStore} /&gt;,但你声明const asyncAdd = ...。我假设asyncAdd 应该是asyncAddStore

    做出这样的假设,我认为您的问题可能不在您提供的文件范围内:

    1. 确保您的 &lt;Switch&gt; 组件位于 &lt;Router&gt;(或 &lt;BrowserRouter&gt; 组件)内...您的 App.js 的父组件中可能有一个/p>

    2. 还要确保您正在渲染您的 App 组件(您需要像上面一样将您的 &lt;Switch&gt; 包装在 &lt;Router&gt; 中)或者您正在渲染 &lt;App&gt;'s父组件,像这样...

      ReactDOM.render( , document.getElementById('root') );

    【讨论】:

    • 道具正确进入课堂。当按预期进行身份验证时,路由 /login 和 /signup 也不可见。但是 /add 不起作用。
    • 我已经根据您的评论更新了我的答案 - 希望这会有所帮助!如果这不起作用,如果您可以提供asyncComponent 的实现,将会有所帮助。
    • 感谢您的回复。我已经确保 内部的 内部。我添加了 asyncComponent.js。是的,它是 asyncAddStore。
    • 你也在顶层调用ReactDOM.render(),对吗?
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 2017-06-07
    • 2017-11-15
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多