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