Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locally instantiated model causing "Failed to resolve reference" error #411

Open
KashubaK opened this issue Jan 29, 2023 · 0 comments
Open

Comments

@KashubaK
Copy link

I have two models: CheckoutModel and ItemModel. I want to be able to create a local CheckoutModel to add ItemModels to before saving. However on my first try, after adding an item to my checkout.items and executing a view on the checkout to calculate the total cost, this error was produced:

Failed to resolve reference 63d6f382a0afdb03a9a1b53d to late(() => ItemModel)

It appears that locally created models are detached from the RootStore which is causing the error (which makes total sense.) The workaround is to manually set the store on the new instance:

import { rootStore } from 'wherever/your/rootStore/is';

const Cart = observer(() => {
  const checkout = useLocalObservable(() => CheckoutModel.create({ id: 'X' }));

  checkout.__setStore(rootStore);

  // blah blah blah
});

This is weird, and makes me feel like I shouldn't be doing this at all. I suppose I should just be creating a new model to only be used for local state, but I don't want to wind up having to do this for every applicable model in my application. For now I'll be abstracting this behind some useLocalInstance hook, or within an afterCreate in my models.

Any thoughts are definitely appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant