【发布时间】:2020-03-12 01:10:04
【问题描述】:
我正在尝试使用 react-router-dom 创建受保护的路由。
奇怪的是错误说:
元素类型无效:应为字符串(用于内置组件) 或类/函数(用于复合组件)但得到:对象。
检查
Context.Consumer的渲染方法。
这就是堆栈跟踪——奇怪的是关于期望字符串的错误?我不是在history.push 中传递"/profile" 吗??
我说这很奇怪的原因是因为在开发工具的控制台中我得到了这个:
index.js:1 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: <Connect(withRouter(LinkNavWithLayout)) />. Did you accidentally export a JSX literal instead of a component?
in Route (created by PrivateRoute)
in PrivateRoute (created by App)
in Switch (created by App)
in App (created by Context.Consumer)
in withRouter(App) (created by Connect(withRouter(App)))
in Connect(withRouter(App)) (created by MyApp)
in Container (created by MyApp)
in PersistGate (created by MyApp)
in Provider (created by MyApp)
in MyApp (created by AppWithReactRouter)
in Router (created by BrowserRouter)
in BrowserRouter (created by AppWithReactRouter)
in AppWithReactRouter (created by AppWithRedux)
in AppWithRedux
in Suspense (created by AppContainer)
in Container (created by AppContainer)
in AppContainer
对我来说这更有意义,因为我觉得问题出在受保护的路由组件中,即意味着我的受保护路由功能中的逻辑或我使用它的方式......
const PrivateRoute = ({ component: Component, children, ...rest }) => (
<Route {...rest} render={props => {
console.log("In PrivateRoute isLoggedIn ", isLoggedIn);
return isLoggedIn === true
? <Component {...props} >{children}</Component>
: <Redirect to='/' />
}} />
)
这就是我使用它的方式。
return (
<>
<Switch>
<Route
path='/'
exact
render={(props) => <LinkNavWithLayout {...props} data={navBars}><Index {...props} /></LinkNavWithLayout>} />
<Route
path='/login'
render={(props) => <Login {...props} />}
/>
<Route
path='/register'
render={(props) => <Register {...props} />}
/>
<PrivateRoute
path='/profile'
isLoggedIn={isLoggedIn}
component={
<LinkNavWithLayout data={navBars}><Profile /></LinkNavWithLayout>
}
/>
<PrivateRoute
path='/dashboard'
isLoggedIn={isLoggedIn}
component={
<LinkNavWithLayout data={navBars}><Dashboard /></LinkNavWithLayout>
}
/>
{/* <Route component={()=> <h1>Not found</h1>} /> */}
</Switch>
</>
)
}
那么为什么我的页面根本没有呈现,这两个错误有什么关系呢?任何帮助将不胜感激!
更新
我添加了 fosso 的建议...
const PrivateRoute = ({ component, isLoggedIn, ...rest }) => (
<Route {...rest} render={props => {
console.log("In PrivateRoute isLoggedIn ", isLoggedIn);
return isLoggedIn === true
? { component }
: <Redirect to='/' />
}} />
)
return (
<>
<Switch>
<Route
path='/'
exact
render={(props) => <LinkNavWithLayout {...props} data={navBars}><Index {...props} /></LinkNavWithLayout>} />
<Route
path='/login'
render={(props) => <Login {...props} />}
/>
<Route
path='/register'
render={(props) => <Register {...props} />}
/>
<PrivateRoute
path='/profile'
isLoggedIn={isLoggedIn}
component={({ children }) => ( // not sure where you're planning on passing `children` from `PrivateRoute`
<LinkNavWithLayout data={navBars}><Profile /></LinkNavWithLayout>
)}
/>
<PrivateRoute
path='/dashboard'
isLoggedIn={isLoggedIn}
component={({children}) => ( // not sure where you're planning on passing `children` from `PrivateRoute`
<LinkNavWithLayout data={navBars}><Dashboard /></LinkNavWithLayout>
)}
/>
{/* <Route component={()=> <h1>Not found</h1>} /> */}
</Switch>
</>
)
}
}
但现在出现以下错误:
Objects are not valid as a React child (found: object with keys {component}). If you meant to render a collection of children, use an array instead.
in Route (created by PrivateRoute)
in PrivateRoute (created by App)
in Switch (created by App)
in App (created by Context.Consumer)
in withRouter(App) (created by Connect(withRouter(App)))
in Connect(withRouter(App)) (created by MyApp)
in Container (created by MyApp)
in PersistGate (created by MyApp)
in Provider (created by MyApp)
in MyApp (created by AppWithReactRouter)
in Router (created by BrowserRouter)
in BrowserRouter (created by AppWithReactRouter)
in AppWithReactRouter (created by AppWithRedux)
in AppWithRedux
in Suspense (created by AppContainer)
in Container (created by AppContainer)
in AppContainer
▶ 30 stack frames were collapsed.
callback
仍然指向...
【问题讨论】:
-
开场
<>和闭幕</>在你的回报中做什么?这是哪个班的? -
@sallf 这是 React.Fragment 的简写。所以你不必到处都有非语义..但是我认为它无论如何都会呈现为LOL
标签: javascript reactjs react-router-dom