失败的道具类型:提供给`Route`的类型`object`的无效道具`component`,期望的`function`

Pom*_*utZ 7 javascript reactjs react-router

我刚刚将我的React应用程序更新为16.6.0并将反应脚本更新为2.0.3以开始使用懒惰,并且在关注官方文档的示例时出现此错误:

失败的道具类型:提供component的类型的无效道具,预期objectRoutefunction

忽略它一切似乎都有效,除了控制台中的这个错误.

这是我的一些代码:

// imports here
... 
const Decks = lazy(() => import('./pages/Decks'));
...
class App extends Component {
      ...

    render() {
        return (
            <ConnectedRouter history={history}>
                <div>
                    <MenuAppBar />

                    <div style={{paddingTop: '4rem'}}>
                        <Suspense fallback={<LazyLoading />}>
                            <Switch>
                                <Route exact path="/" component={Home} />
                                <Route path="/decks" component={Decks} />
                                ...
                            </Switch>
                        </Suspense>
                    </div>

                    <Footer />
                </div>
            </ConnectedRouter>
        );
    }
Run Code Online (Sandbox Code Playgroud)

...}

我在这里做错了什么?

Shu*_*tri 6

使用延迟加载的组件时,您需要将其提供给Route组件,例如

// imports here
... 
const Decks = React.lazy(() => import('./pages/Decks'));
...
class App extends Component {
      ...

    render() {
        return (
            <ConnectedRouter history={history}>
                <div>
                    <MenuAppBar />

                    <div style={{paddingTop: '4rem'}}>
                        <Suspense fallback={<LazyLoading />}>
                            <Switch>
                                <Route exact path="/" component={Home} />
                                <Route path="/decks" render={(props) => <Decks {...props} />} />
                                ...
                            </Switch>
                        </Suspense>
                    </div>

                    <Footer />
                </div>
            </ConnectedRouter>
        );
    }
... 
}
Run Code Online (Sandbox Code Playgroud)

可能是在react-router中的PropType检查不正确,并且可能已在最新版本中进行了修复,以使其与react v16.6兼容


小智 5

将“ react-router-dom”更新为“ ^ 4.4.0-beta.6”,将其修复。

这是一个错误:https : //github.com/ReactTraining/react-router/issues/6420#issuecomment-435171740