Next.js Discord

Discord Forum

Page re-renders on navigation within a slug

Unanswered
shadow_aya posted this in #help-forum
Open in Discord
NextJS version: 14

Hello. I have a slug page on my website. The reason is that I have two similar pages, and navigating between the two only updates state in a Context, otherwise the two pages have identical structure and purpose:

src/app/stratle/[mode]/page.tsx
export default function Page() {

    return (
        <section className={styles.page}>
            <div>
                <Inputs />
            </div>
            <div>
                <StratagemPanel />
                <Order />
            </div>
        </section>
    );
}


Some parts of the component(s) sort through a pretty large list, which is expensive, so I did this to avoid re-renders - but they happen anyway.
I verified this by putting a log inside StratagemPanel's useEffect, and navigation fires it, even in prod. I navigate via next's Link component.

Any idea how to prevent re-rendering here, or do I have to switch to a URL search parameter?

3 Replies

after rethinking this, I don't think this is possible. I added my own solution with window.history.pushState, but if someone has a better idea, I'd appreciate any suggestions
Filipino Venus
Hi @shadow_aya
Could you please let me know how you solved the issue with window.history.pushState?

I'm having a similar issue with you now.
I'm showing products in the same route: /categories/[slug]/[page]/.
When I navigate the pages in the same slug, for example: /categories/apple/1/, /categories/apple/2/, .../categories/apple/10/, etc.
For this reason, I'm losing the previous page's content.
The page is re-rendering because the page param is changing. I know it's impossible to prevent the re-render.
I would like to know how you resolve the issue.
Thanks