next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Shallow Routing incorrectly recreate/remount component (lose state)

Open ivanwidj opened this issue 3 years ago • 6 comments

Run next info (available from version 12.0.8 and up)

No response

What version of Next.js are you using?

12.0.8 (also tried with 11.1.3)

What version of Node.js are you using?

16.5.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start, Netlify

Describe the Bug

First shallow routing after redirect from another page will incorrectly remount component (any component state will be reset). Subsequent shallow routing within the same page works as expected (keeping component state).

Expected Behavior

Shallow routing within the same page should keep component state

To Reproduce

  • create 2 pages (i.e. page A and B)

  • In page B component, add some local state

const [value, setValue] = useState('initial');
  • From page A redirect to page B
router.push({ pathname: '/B/[slug], query: { slug: '1' } })
  • In page B, update the local component state
setValue('updated');

and then do shallow routing to update the slug

router.push({ pathname: '/B/[slug], query: { slug: '2' } })

the state value will reset to initial (component recreated).

  • Subsequent shallow routing works as expected and keep the state value

ivanwidj avatar Jan 22 '22 01:01 ivanwidj

any update on this?

ivanwidj avatar Feb 18 '22 21:02 ivanwidj

I'm having the exact same issue with my shallow routing on tab components. Any update on this?

jiyonghong avatar May 15 '22 16:05 jiyonghong

I am experiencing same issue on 12.2.0

ajjack50n avatar Aug 20 '22 14:08 ajjack50n

We're experiencing the same issue on dynamic paths. Still investigating on our end.

Multiply avatar Sep 12 '22 05:09 Multiply

I am having the exact same issue with dynamic paths and shallow routing. (v12.3.0)

alc0034 avatar Sep 15 '22 15:09 alc0034

It turns out that our issue was related to an older version of next-i18next. When pushing changes to the router, it would unset the locale of the i18next provider, causing the whole page to re-render, losing state.

After updating to the latest version, it keeps the locale around, and this issue went away.

Multiply avatar Sep 16 '22 07:09 Multiply

It turns out that our issue was related to an older version of next-i18next. When pushing changes to the router, it would unset the locale of the i18next provider, causing the whole page to re-render, losing state.

After updating to the latest version, it keeps the locale around, and this issue went away.

I use newest version of next-translate (1.6.0) but this lose state problem still happen. Any suggest ?

mfnash avatar Sep 23 '22 07:09 mfnash

It turns out that our issue was related to an older version of next-i18next. When pushing changes to the router, it would unset the locale of the i18next provider, causing the whole page to re-render, losing state.

After updating to the latest version, it keeps the locale around, and this issue went away.

@Multiply thanks for this. I was able to track that my whole _app was getting remounted, but I didn't think of appWithTranslation wrapper being the culprit. For anyone looking for more information, they fixed it in v10.3.0: https://github.com/i18next/next-i18next/releases/tag/v10.3.0 (I actually tested 10.2.0 vs 10.3.0 to see the difference.)

rtrembecky avatar Oct 18 '23 16:10 rtrembecky