Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've never been able to get hot reloading to work with
useElmish
so I usually just use regular global style Elmish.However I wanted to try out vite and Elmish.HMR doesn't currently work with vite so I decided to have another look at
useElmish
. This PR makes it work as expected with hot reloading, at least when testing a simple counter with vite+preact (which uses prefresh).From my testing it seems like state in
useState
is preserved between hot reloads butuseRef
is not. TheuseEffect
which resets the state and runs cmds also gets executed when doing a hot reload. This change makes it so that thestate
ref is initialised tochildState
so that it is preserved between hot reloads.useEffect
checksisFirstRender
before reinitialising state; because isFirstRender is a useRef, it will get reset to true during a hot reload so that it doesn't reset the state.I tried testing it with react as well but had so many other issues with react-refresh and using hooks and fable. I think something to do with having extra
export function ...
in the same js file as the component might be messing up react-refresh somehow. So if anyone can get it working with react let me know :). So that is to say IDK if the behaviour of useRef and useState is the same with react-refresh as it is with prefresh but hopefully it is.I've marked it as WIP because I need help on making it handle the initial
Cmd
correctly. See, the current behaviour on hot reloads is to reset the state and run inital Cmd (from init). This PR currently changes it so that on hot reloads it doesn't reset the state but does still run the initial Cmd which is (probably?) not good.