Skip to content

Commit 175e57e

Browse files
committed
better document HMR behaviour
1 parent 6c61ddf commit 175e57e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,31 @@ It makes sense: all those modules boast of being able to apply a code change to
6363

6464
Here you are. This is what HMR is, and how it works.
6565

66+
I think the most important take away is that the component that is affected by a change will be recreated. Its state will be preserved but all its children will be recreated too, and their state not preserved. This is a necessity because the elements / child components structure of a component can be entirely different after the file has been modified (and we don't really have the technical capacity to track children).
67+
6668
Now, the best way to see what it can do for you is probably to checkout the template bellow and get your hands at it! (Add 500 components and try Nollup, you should love the speed!)
6769

70+
### Preservation of local state
71+
72+
Local state is preserved by Svelte HMR, that is any state that Svelte itself tracks as reactive (basically any root scope `let` vars, exported or not).
73+
74+
This means that in code like this:
75+
76+
~~~svelte
77+
<script>
78+
let x = 1
79+
x++ // x is now 2
80+
</script>
81+
82+
<p>{x}</p>
83+
~~~
84+
85+
If you replace `let x = 1` by `let x = 10` and save, the previous value of `x` will be preserved. That is, `x` will be 2 and not 10. The restoration of previous state happens _after_ the init code of the component has run, so the value will not be 11 either, despite the `x++` that is still here.
86+
87+
If you find this behaviour inconvenient, you can disable it by setting `noPreserveState` option of your HMR-enabled bundler plugin.
88+
89+
If you want to disable it for just one particular file, or just temporarily, you can turn it off by adding a `// @!hmr` comment somewhere in your component.
90+
6891
## Svelte HMR tools
6992

7093
### Rollup / Nollup

0 commit comments

Comments
 (0)