警告:你不应该使用 <Route component>和<路由渲染>在同一条路线上;<路由渲染>将被忽略 [英] Warning: You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored

查看:37
本文介绍了警告:你不应该使用 <Route component>和<路由渲染>在同一条路线上;<路由渲染>将被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,如果没有身份验证,我尝试保护路由,但它不起作用

Hello i try to protected route if is no auth but it is not work

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

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

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);

警告:您不应在同一路由中使用路由组件"和路由渲染";路由渲染"将被忽略

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

我该如何解决?

推荐答案

来自 路由渲染方法:

有 3 种使用 渲染内容的方法:

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 contains both component and render prop. You can only use one rendering method but not both.

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

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

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

FIX : Rename component prop to comp since it's acting as an 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} />
      )
    }
  />
);

这篇关于警告:你不应该使用 &lt;Route component&gt;和&lt;路由渲染&gt;在同一条路线上;&lt;路由渲染&gt;将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆