admin管理员组文章数量:1405621
I need to add slots to a PageContainer, specific to each page, so I need to get the title of the page first. I'm following example in MUI Toolpad for dynamic routes in PageContainer component:
/
It looks like this:
export default function Example() {
const params = useParams<{ id: string }>();
const activePage = useActivePage();
invariant(activePage, 'No navigation match');
const title = `Item ${params.id}`;
const path = `${activePage.path}/${params.id}`;
const breadcrumbs = [...activePage.breadcrumbs, { title, path }];
return (
<PageContainer title={title} breadcrumbs={breadcrumbs}>
...
</PageContainer>
);
}
But when I try to do something similar, like this:
const activePage = useActivePage(); //returns null
console.log("Nav: activePage=", activePage);
invariant(activePage, 'No navigation match');
const path = activePage ? `${activePage.path}` : ''; //returns empty string
console.log("Nav: path=", path);
return (
<ReactRouterAppProvider
navigation={NAVIGATION}
branding={BRANDING}
>
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>
</ReactRouterAppProvider>
)
useActivePage() returns null. Has anybody else run into this issue and got it to work?
I need to add slots to a PageContainer, specific to each page, so I need to get the title of the page first. I'm following example in MUI Toolpad for dynamic routes in PageContainer component:
https://next.mui/toolpad/core/react-page-container/
It looks like this:
export default function Example() {
const params = useParams<{ id: string }>();
const activePage = useActivePage();
invariant(activePage, 'No navigation match');
const title = `Item ${params.id}`;
const path = `${activePage.path}/${params.id}`;
const breadcrumbs = [...activePage.breadcrumbs, { title, path }];
return (
<PageContainer title={title} breadcrumbs={breadcrumbs}>
...
</PageContainer>
);
}
But when I try to do something similar, like this:
const activePage = useActivePage(); //returns null
console.log("Nav: activePage=", activePage);
invariant(activePage, 'No navigation match');
const path = activePage ? `${activePage.path}` : ''; //returns empty string
console.log("Nav: path=", path);
return (
<ReactRouterAppProvider
navigation={NAVIGATION}
branding={BRANDING}
>
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>
</ReactRouterAppProvider>
)
useActivePage() returns null. Has anybody else run into this issue and got it to work?
Share Improve this question asked Mar 24 at 17:24 user3217883user3217883 1,4857 gold badges48 silver badges81 bronze badges1 Answer
Reset to default 1Turns out that useActivePage, a hook, has to be placed in a function outside of the main export default function. Like the example, I created a function called Content() which made the call to useActivePage, like this:
function Content() {
const activePage = useActivePage();
console.log("Content: activePage=",activePage);
invariant(activePage, 'No navigation match');
return (
<PageContainer >
<Outlet />
</PageContainer>
);
}
And then implemented it in the main function like:
return (
<ReactRouterAppProvider
navigation={NAVIGATION}
branding={BRANDING}
>
<DashboardLayout>
<Content/>
</DashboardLayout>
</ReactRouterAppProvider>
)
Now it returns the expected output:
Content: activePage= {title: 'Physiology', path: '/home/physiology', breadcrumbs: Array(2)}
I don't know why it only works in a function external to the main function.
本文标签: javascriptWhy does useActivePage() in React MUI Toolpad example return nullStack Overflow
版权声明:本文标题:javascript - Why does useActivePage() in React MUI Toolpad example return null? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744237422a2596620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论