Created
March 20, 2020 09:19
-
-
Save kitze/70a3488163d2f849a963c361cdb79479 to your computer and use it in GitHub Desktop.
mst-utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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