[英]React Router with React 16.6 Suspense “Invalid prop `component` of type `object` supplied to `Route`, expected `function`.”
I'm using the latest version (16.6) of React with react-router
(4.3.1) and trying to use code splitting using React.Suspense
. 我正在将React的最新版本(16.6)与
react-router
(4.3.1)一起使用,并尝试通过React.Suspense
使用代码拆分。
Although my routing is working and the code did split into several bundles loaded dynamically, I'm getting a warning about not returning a function, but an object to Route
. 尽管我的路由正常工作,并且代码确实分成了多个动态加载的捆绑包,但我收到警告,提示不要返回函数,而是返回
Route
的对象。 My code: 我的代码:
import React, { lazy, Suspense } from 'react';
import { Switch, Route, withRouter } from 'react-router-dom';
import Loading from 'common/Loading';
const Prime = lazy(() => import('modules/Prime'));
const Demo = lazy(() => import('modules/Demo'));
const App = () => (
<Suspense fallback={<Loading>Loading...</Loading>}>
<Switch>
<Route path="/" component={Prime} exact />
<Route path="/demo" component={Demo} />
</Switch>
</Suspense>
);
export default withRouter(App);
The console warning is as follows: Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`.
控制台警告如下:
Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`.
A normal import would return a function, but the dynamic import with lazy()
is returning an object. 普通导入将返回一个函数,但是带有
lazy()
的动态导入将返回一个对象。
Any fixes for this? 有什么解决办法吗?
Try using render
prop instead of component
尝试使用
render
道具代替component
<Route path="/" render={()=> <Prime />} exact />
<Route path="/demo" render={()=> <Demo />} />
This will be fixed in react-router-dom
version 4.4+ as this issue suggests 此问题将在
react-router-dom
版本4.4+中修复此问题
You can wait for the final release or if you don't want to change your code today, you can install the beta version now by yarn add react-router-dom@next
您可以等待最终版本,或者如果您不想今天更改代码,则可以通过
yarn add react-router-dom@next
立即安装Beta版本。
I know this answer do not respond the original question, but as I have experienced a similar issue, maybe my solution will help another people. 我知道这个答案并不能回答最初的问题,但是由于我遇到过类似的问题,也许我的解决方案会对其他人有所帮助。
My Error: 我的错误:
Failed prop type: Invalid prop `component` of type `object` supplied to "link", expected function.
Alike the accepted answer, this could be fixed with: 类似于已接受的答案,可以通过以下方式解决此问题:
<Link to={'/admin'} title={'Log In'} component={props => <Button {...props} />} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.