I have some help pages in my app. The route looks like this:
<Route exact path={`${routeTabHelp}/:helpCode`}>
<HelpDisplayIndex />
</Route>
Here is HelpDisplayIndex
, which uses react-query
to fetch a JSON object that contains all the pages of help documentation.:
const HelpDisplayIndex: React.VFC = () => {
const platform = useContext(PlatformContext);
const { data } = useHelpPages(platform);
return (
<Suspense fallback={<Loading />}>
<PageHelp data={data} />
</Suspense>
);
};
export default React.memo(HelpDisplayIndex);
And then PageHelp
, which uses useParams
to fetch the page code from the query string in the URL, which is also the key to the page data in the JSON object fetched by react-query
in HelpDisplayIndex
above:
const PageHelp: React.VFC<MyProps> = ({
data,
}: MyProps) => {
const { helpCode } = useParams<{ helpCode: string }>();
// If verifyNot404 fails, it returns a 404 page.
const page = verifyNot404(helpCode, data);
useIonViewDidEnter(() => {
const selector = `ion-content.${helpCode}`;
const body = document.querySelector(selector);
if (body) {
body.scrollIntoView();
console.log('scrolled!');
}
}, [helpCode]);
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonBackButton />
</IonButtons>
<IonTitle>{ page.title }</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent className={`${classesPageWithBodyText} ${helpCode}`}>
{ page.body }
<ToolbarHelp pageFeedsCode={helpCode} helpPageData={data} />
</IonContent>
</IonPage>
);
};
export default PageHelp;
Here, <ToolbarHelp>
contains next and previous links between the help pages.
The problem is that when I navigate between help pages using the next and previous links, I am not returned to the top of the page. Instead, after pressing the button, I am usually at the bottom of the page, or half-way down a long page.
In console.log()
, I can see that useIonViewDidEnter()
is firing, and I see the scrolled!
output. So it seems that something is preventing the page from scrolling.
Does Ionic block scrolling somehow? I want the user to always start at the top of the page when a page is loaded; it makes no sense to click a “next” button and then be sent to the middle of the next page.