Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

how can I fix it ?

like image 284
HelloGello Avatar asked Aug 08 '19 08:08

HelloGello


2 Answers

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

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} />
      )
    }
  />
);
like image 96
Joseph D. Avatar answered Nov 10 '22 04:11

Joseph D.


Use <Route render={() => <YourComponent />} /> instead of <Route component={YourComponent} />.

Don't combine two of those, react does not like that.

like image 4
sebamed Avatar answered Nov 10 '22 05:11

sebamed