inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.03k stars 405 forks source link

History back shouldn't trigger full rerender if Inertia's state didn't change #1821

Closed artygrand closed 1 month ago

artygrand commented 4 months ago

Version:

Describe the problem:

Currently I rewrite my modal manager from react-router, where everything works fine. I use history to change private state, and Inertia rerenders whole page, even I didn't change its state

Steps to reproduce:

When I open modal I call history.pushState({...history.state, modal: modal.id}, ''); hence I keep all Inertia data. Later, when I click browser back button or call history.go(-1) I close modal on POP event.

But with this action, current page fully rerenders all components and of cource triggers useEffect(() => {}, []) Thankfully, it's coming at least without calling to backend.

Thank you!

shengslogar commented 4 months ago

Inertia has history-based state management built in. You'll probably have better luck using that system instead of building your own.

https://inertiajs.com/remembering-state#manually-saving-state

artygrand commented 4 months ago

@shengslogar, problem not with data storing, but with returning back without side effect. router.remember(data, 'my-key') is replaces state, not pushes new, as I just tested. I can't return back to same page with it

reinink commented 1 month ago

Hey! Manually using history.pushState is not currently supported with Inertia.js.

That said I hope to eventually add the ability to make client-side only visits using an API like Inertia.push().

My recommendation right now would be to use Inertia props to open/close a dialog like this. Yes that means hitting the server, but, at least at this time, that's the "Inertia way" 👍

artygrand commented 1 month ago

Thanks for giving us hope for client-side visits. This feature will fix more problems, such as compatibility with use-query-params