admin管理员组文章数量:1279055
I'm trying to use react-router-dom v6.4+ in my project. I implemented it as a route array of objects. Its worked as routing but suddenly I got another issue realated this. I can't call any hook inside the Component which located on element
property in route array.
In the route.ts
file:
import MainLayout from './container/layouts/mainLayout/MainLayout'
import ErrorPage from './view/Error'
import Home from './view/Home'
const routes: RouteObject[] = [
{
path: '/',
element: MainLayout(),
children: [
{
index: true,
element: Home(),
},
],
},
{
path: '*',
element: ChangeRoute('/404'),
},
{
path: '/404',
element: ErrorPage(),
},
]
const router = createBrowserRouter(routes)
export default router
and in the app.ts
file:
<RouterProvider router={router} fallbackElement={<React.Fragment>Loading ...</React.Fragment>} />
But If I try to use any hook , inside MainLayout ponent , its saying
code in MainLayout
ponent :
const MainLayout = () => {
const [collapsed, setCollapsed] = useState(false)
return (
<Layout className='layout'>
<SideBar collapsed={collapsed} />
<Layout>
<Topbar collapsed={collapsed} setCollapsed={setCollapsed} />
<Outlet />
</Layout>
</Layout>
)
}
export default MainLayout
I think if I use element: <MainLayout/>
instead of element: MainLayout()
, then this issue will resolve. but typescript doesnt allow me to do this. and on the documentation every thing is on plain javascript. only one type defination there is this
How to solve this? Kindly guide me.
Edit Here is the codesandbox demo : visit sandbox
I'm trying to use react-router-dom v6.4+ in my project. I implemented it as a route array of objects. Its worked as routing but suddenly I got another issue realated this. I can't call any hook inside the Component which located on element
property in route array.
In the route.ts
file:
import MainLayout from './container/layouts/mainLayout/MainLayout'
import ErrorPage from './view/Error'
import Home from './view/Home'
const routes: RouteObject[] = [
{
path: '/',
element: MainLayout(),
children: [
{
index: true,
element: Home(),
},
],
},
{
path: '*',
element: ChangeRoute('/404'),
},
{
path: '/404',
element: ErrorPage(),
},
]
const router = createBrowserRouter(routes)
export default router
and in the app.ts
file:
<RouterProvider router={router} fallbackElement={<React.Fragment>Loading ...</React.Fragment>} />
But If I try to use any hook , inside MainLayout ponent , its saying
code in MainLayout
ponent :
const MainLayout = () => {
const [collapsed, setCollapsed] = useState(false)
return (
<Layout className='layout'>
<SideBar collapsed={collapsed} />
<Layout>
<Topbar collapsed={collapsed} setCollapsed={setCollapsed} />
<Outlet />
</Layout>
</Layout>
)
}
export default MainLayout
I think if I use element: <MainLayout/>
instead of element: MainLayout()
, then this issue will resolve. but typescript doesnt allow me to do this. and on the documentation every thing is on plain javascript. only one type defination there is this
How to solve this? Kindly guide me.
Edit Here is the codesandbox demo : visit sandbox
Share Improve this question edited Nov 29, 2022 at 5:44 Arijit asked Nov 29, 2022 at 5:01 ArijitArijit 1341 silver badge8 bronze badges 8- The react docs state that hooks can only be called in ponents , but you are using the ponent as a function which is wrong, you have to pass in your ponent as an element – Dev Man Commented Nov 29, 2022 at 5:03
- Thats what I'm looking for, How to do that? in js ,I can do this, but here ts doesnt allowing me. – Arijit Commented Nov 29, 2022 at 5:04
- what is the error that typescript is giving you – Dev Man Commented Nov 29, 2022 at 5:07
-
type MainLayout = /*unresolved*/ any 'MainLayout' refers to a value, but is being used as a type here. Did you mean 'typeof MainLayout'?ts(2749)
– Arijit Commented Nov 29, 2022 at 5:08 -
Hmm, other than the incorrect routes config, this seems more like an issue specifically in the
MainLayout
file. Can you edit the post to include a plete minimal reproducible example for us to inspect, along with the plete error message or linting error text and the line of code causing the issue? – Drew Reese Commented Nov 29, 2022 at 5:10
2 Answers
Reset to default 10Changes the name of the route.ts
file to route.tsx
, now you can will set ponents in the element
object property, this works for me.
The element
prop expects a React.ReactNode
, you are directly calling a React function instead of passing it as JSX.
Example:
const routes: RouteObject[] = [
{
path: '/',
element: <MainLayout />,
children: [
{
index: true,
element: <Home />,
},
],
},
{
path: '*',
element: <ChangeRoute redirect='/404' />,
},
{
path: '/404',
element: <ErrorPage />,
},
]
本文标签: javascriptHow to use React router v64 with typescript properlyStack Overflow
版权声明:本文标题:javascript - How to use React router v6.4 with typescript properly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741261090a2367633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论