简体   繁体   English

提供的道具类型无效

[英]Invalid prop type supplied

I have a React route defined in my app as:我在我的应用程序中定义了一个 React 路由:

<PrivateRoute path="/home" component={Home} />

The component loads the user from the store then depending on the type does a redirect.该组件从商店加载用户,然后根据类型进行重定向。 It is defined as:它被定义为:

import React, { useEffect, memo } from 'react'
import { connect } from 'react-redux'
import { compose } from 'redux'
import { createStructuredSelector } from 'reselect'
import { Redirect } from 'react-router-dom'
import { useInjectSaga } from '../../utils/injectSaga'
import {
  makeSelectCurrentUser,
  makeSelectLoading,
  makeSelectError
} from '../../containers/App/selectors'
import { loadUser } from '../../containers/App/actions'
import saga from '../../components/Header/saga'

const key = 'home'

function Home({ user, loading, error, loadUser }) {
  useInjectSaga({ key, saga })

  const username = localStorage.getItem('username')
  useEffect(() => {
    loadUser(username)
  }, [])

  return !loading && user && !error ? (
    <Redirect to={user.type === 'admin' ? '/admin' : '/promoter'} />
  ) : (
    <div>Loading...</div>
  )
}

const mapStateToProps = createStructuredSelector({
  user: makeSelectCurrentUser(),
  loading: makeSelectLoading(),
  error: makeSelectError()
})

const mapDispatchToProps = dispatch => {
  return {
    loadUser: username => dispatch(loadUser(username))
  }
}

const withConnect = connect(mapStateToProps, mapDispatchToProps)

export default compose(withConnect, memo)(Home)

When I execute the code, it works.当我执行代码时,它起作用了。 However, I get a warning in the console:但是,我在控制台中收到警告:

index.js:1437 Warning: Failed prop type: Invalid prop `component` supplied to `PrivateRoute`.

It points to the line with my "home" route.它指向我的“家”路线。

What am I doing wrong?我究竟做错了什么? How should this be done?这应该怎么做?

I found the solution.我找到了解决方案。 Once I did the below, the problem went away.一旦我做了下面的事情,问题就消失了。

<PrivateRoute path="/home" component={() => <Home/>} />

Still weird, yet the latter is obviously valid no matter what.仍然很奇怪,但无论如何后者显然是有效的。 Seems like a bug in react-router, maybe.似乎是 react-router 中的一个错误,也许吧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败的道具类型:提供给“重定向”的无效道具“到” - Failed prop type: Invalid prop `to` supplied to `Redirect` 提供给“对话框”的“功能”类型的无效道具“打开” - Invalid prop `open` of type `function` supplied to `Dialog` 失败的道具类型:提供给“TextInput”的“对象”类型的道具“值”无效 - Failed prop type: Invalid prop 'value' of type 'object' supplied to 'TextInput' 道具类型失败:提供给“输入”的值“搜索”无效的道具“类型” - Failed prop type: Invalid prop `type` of value `search` supplied to `Input` 失败的道具类型:提供给`GlobalState`的`array`类型的无效道具`children` - Failed prop type: Invalid prop `children` of type `array` supplied to `GlobalState` 反应警告:失败的道具类型:提供的“对象”类型的无效道具 - React Warning: Failed prop type: Invalid prop of type `Object` supplied 失败的道具类型:提供给“RCTView”的“对象”类型的无效道具“不透明度” - Failed prop type: Invalid prop `opacity` of type `object` supplied to `RCTView` 警告:道具类型失败:提供给“路线”的道具属性无效。 在途中 - Warning: Failed prop type: Invalid prop `component` supplied to `Route`. in Route 道具类型失败:提供给CardHeader的道具openIcon无效,应该是ReactNode - Failed prop type: Invalid prop openIcon supplied to CardHeader, expected a ReactNode 警告:道具类型失败:提供给“ DropdownItem”的道具“ as”无效 - Warning: Failed prop type: Invalid prop `as` supplied to `DropdownItem`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM