sveltejs / svelte-hmr

HMR commons for Svelte 3
ISC License
204 stars 20 forks source link

support preserving state of children component when updating parent component #40

Open bloody-ux opened 3 years ago

bloody-ux commented 3 years ago

Describe the issue

When updating the code of parent component, the local state of children component is lost(with preserveLocalState: true). While the local state of parent component can be kept.

In below screenshot, the local state of Counter will reset to default when App is change. Because in svelte-hmr, when parent component is changed, it will recreate itself(can keep local state) and it's children components(without keeping local state).

image

Expected behavior

The local state of children is kept as well when updating parent component, just like react does. It's annoying when debugging between parent component and children components if the local state is lost.

And developer may think it's a bug of HMR because of the inconsistence.

rixo commented 2 years ago

Hey! Some progress update...

I have studied how feasible this could be, and came up with some (dirty/incomplete) POC here: https://github.com/rixo/svelte-hmr/blob/poc-preserve-children/PRESERVE_CHILDREN_STATE.md#demo.

However, this is probably not something that can be done sanely by externally patching Svelte's compiler output like this package currently does. So HMR will have to be integrated into the compiler first. I have started looking into it, but that's probably still quite a long shot...