mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

About getRoot #1965

Closed dodoto closed 2 years ago

dodoto commented 2 years ago

Question

I have two store provider with no relationship.

const StoreA = ModelA.create()
const StoreB = ModelB.create()

<ProviderA value={StoreA}>
   {...elements}
   <ProviderB value={StoreB}>
      {...elements}
   </ProviderB>
</ProviderA>

When I use getRoot(StoreB.child), it always return StoreA. If I want get StoreB, what should I do?

mattruby commented 2 years ago

Can you create a codesandbox example please.

On Mon, Sep 26, 2022 at 4:34 AM AD2012 @.***> wrote:

Question

I have two store provider with no relationship.

const StoreA = ModelA.create() const StoreB = ModelB.create()

{...elements} {...elements}

When I use getRoot(StoreB.child), it always return StoreA. If I want get StoreB, what should I do?

— Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx-state-tree/issues/1965, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABCW4RMKCSS2RGOTSUNPBTWAFUZVANCNFSM6AAAAAAQVTK5SU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- -Matt Ruby- @.***

dodoto commented 2 years ago

It's my fault. getRoot works correctly. I reviewed my code and found I add store to root store. So when I use getRoot, it always returns root store. Now I use types.reference to solve the issue.