Skip to content

Instantly share code, notes, and snippets.

View jetako's full-sized avatar

Jesse Cooper jetako

View GitHub Profile
@jetako
jetako / useMSTFastRefresh.ts
Last active January 13, 2023 07:20
MobX-State-Tree Fast Refresh hook
import { useRef } from 'react'
import { applySnapshot, getSnapshot, IStateTreeNode } from 'mobx-state-tree'
/**
* Preserves state when fast refresh causes the root store to change.
*/
export function useMSTFastRefresh(rootStore: IStateTreeNode) {
const rootStoreRef = useRef(rootStore)
if (rootStore !== rootStoreRef.current) {