警告:你不应该在同一路由中使用 <Route component> 和 <Route render>; <Route render> 将被忽略

Warning: You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored

你好,如果没有授权,我尝试保护路由,但它不起作用

警告:不要在同一个路由中使用路由组件和路由渲染;路线渲染将被忽略

App.js

  import React, { Fragment, useEffect } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import NavBar from './component/Layout/NavBar';
import Landing from './component/Layout/Landing';
import Login from '../src/component/Auth/Login';
import Register from '../src/component/Auth/Register';
import Dashboard from './component/dashboard/Dashboard';
import Alert from './component/Auth/Alert';
import PrivateRoute from './component/routing/PrivateRoute';

import './App.css';

// Redux
import { Provider } from 'react-redux';
import store from './store';
import setAuthToken from './utils/token';

import { loadUser } from './action/auth';


if (localStorage.token) {
  setAuthToken(localStorage.token);
}
const App = () => {

  useEffect(() => {
    store.dispatch(
      loadUser());
  }, []);

  return (
    <Provider store={store}>
      <Router>
        <Fragment>
          <NavBar />
          <Route exact path="/" component={Landing}></Route>
          <section className="container">
            <Alert />
            <Switch>
              <Route exact path="/login" component={Login}></Route>
              <Route exact path="/register" component={Register}></Route>
              <PrivateRoute exact path="/dashboard" component={Dashboard}></PrivateRoute>
            </Switch>
          </section>
        </Fragment>
      </Router>
    </Provider>
  );
};

export default App;

PrivateRoute.js

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Route, Redirect } from 'react-router-dom';

const PrivateRoute = ({
  componet: Component,
  auth: { isAuthenticated, loading },
  ...rest
}) => (
  <Route
    {...rest}
    render={props =>
      !isAuthenticated && !loading ? (
        <Redirect to="/login" />
      ) : (
        <Component {...props} />
      )
    }
  />
);

PrivateRoute.propTypes = {
  auth: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
  auth: state.auth
});

export default connect(mapStateToProps)(PrivateRoute);

警告:您不应该在同一条路线中使用 "Route component" 和 "Route render"; "Route render" 将被忽略

我该如何解决?

使用 <Route render={() => <YourComponent />} /> 而不是 <Route component={YourComponent} />

不要将其中两个结合起来,react 不喜欢这样。

来自 route rendering method:

There are 3 ways to render something with a <Route>:

- <Route component>
- <Route render>
- <Route children>

Each is useful in different circumstances. You should use only one of these props on a given

PrivateRoute 包含 componentrender 属性。您只能使用一种渲染方法,但不能同时使用

<PrivateRoute exact path="/dashboard" component={Dashboard}></PrivateRoute> // here

const PrivateRoute = ({
  ...
}) => (
  <Route
    render={props => ()} // here
  />
);

修复 :将 component prop 重命名为 comp 因为它充当 HOC:

// rename prop to `comp`
<PrivateRoute exact path="/dashboard" comp={Dashboard}></PrivateRoute>

const PrivateRoute = ({
  comp: Component, // use comp prop
  auth: { isAuthenticated, loading },
  ...rest
}) => (
  <Route
    {...rest}
    render={props =>
      !isAuthenticated && !loading ? (
        <Redirect to="/login" />
      ) : (
        <Component {...props} />
      )
    }
  />
);