Skip to content

Instantly share code, notes, and snippets.

@kitze
Created March 20, 2020 09:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitze/70a3488163d2f849a963c361cdb79479 to your computer and use it in GitHub Desktop.
Save kitze/70a3488163d2f849a963c361cdb79479 to your computer and use it in GitHub Desktop.
mst-utils
import { types, IAnyModelType, Instance, cast } from "mobx-state-tree";
import React, { useMemo } from "react";
export const createModel = <T extends IAnyModelType>(
model: T,
value?: Record<string, any>
) => types.optional(model, () => model.create(value));
export function castSelf<IStoreInstance, IParentModel>(
store: IStoreInstance,
model: IParentModel
) {
return cast<Instance<IParentModel>>(store as Instance<IParentModel>);
}
export const useModel = (Model: any, values) =>
useMemo(() => Model.create(values), []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment