gpt4 book ai didi

reactjs - 警告 : You should not use and in the same route; will be ignored

转载 作者:行者123 更新时间:2023-12-03 13:34:59 30 4
gpt4 key购买 nike

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

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

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 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支持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} />
)
}
/>
);

关于reactjs - 警告 : You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57408430/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com