简体   繁体   English

带有React 16.6 Suspense的React Router“向“ Route”提供了类型为“ object”的无效prop`component`,预期为“ function”。”

[英]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.

相关问题 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 提供给“路线”的“对象”类型的无效道具“组件”,预期的“功能” - Invalid prop `component` of type `object` supplied to `Route`, expected `function` React-Router:失败的prop类型:在route中提供给Route的无效prop`component` - React-Router: Failed prop type: Invalid prop `component` supplied to `Route` in route 反应路由器,多个组件警告:道具类型失败:提供给“路线”的道具“组件”无效 - react router, multiple components warning: Failed prop type: Invalid prop `component` supplied to `Route` 警告:失败的道具类型:提供给“路由”的无效道具“组件”-react-router-dom - Warning: Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom 道具类型失败:提供给“Route”的“object”类型的无效道具“component”,预期为“function” - Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` 失败的道具类型:提供给路线的“对象”类型的无效道具“组件”,应为 function? - Failed prop type: Invalid prop `component` of type `object` supplied to Route, expected function? 警告:道具类型失败:提供给“Route”的“object”类型的无效道具“component”,预期为“function” - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` 反应路由器错误:无效的道具组件提供给路线 - React Router Error: Invalid prop `component` supplied to `Route` react-instantsearch-dom:: 道具类型失败:提供给“Hits”的“object”类型的无效道具“hitComponent”,预期为“function” - react-instantsearch-dom:: Failed prop type: Invalid prop `hitComponent` of type `object` supplied to `Hits`, expected `function`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM