乐闻世界logo
搜索文章和话题

How can I access mobx store in another mobx store?

2个答案

1
2

In MobX, accessing another store from within a store can be achieved through several methods. Here are some common approaches:

1. Dependency Injection via Constructor

When creating a store instance, pass other required stores as parameters. This approach is similar to dependency injection, allowing each store to have references to other stores during initialization.

javascript
class StoreA { constructor(storeB) { this.storeB = storeB; } get someData() { return this.storeB.someOtherData; } } class StoreB { someOtherData = "This is some data from StoreB"; } const storeB = new StoreB(); const storeA = new StoreA(storeB);

In the above example, StoreA receives an instance of StoreB as a parameter during its creation and stores it in its own property. This allows StoreA to easily access data from StoreB.

2. Root Store Pattern

The Root Store pattern involves creating a main store, typically called RootStore, which holds references to all other child stores. Then, each child store can receive the RootStore instance as a parameter in its constructor and access other stores through it.

javascript
class StoreA { constructor(rootStore) { this.rootStore = rootStore; } get someData() { return this.rootStore.storeB.someOtherData; } } class StoreB { someOtherData = "This is some data from StoreB"; } class RootStore { constructor() { this.storeB = new StoreB(); this.storeA = new StoreA(this); } } const rootStore = new RootStore();

With this approach, all stores are connected through the RootStore, and each store can access other store instances within the root store.

3. Using MobX's context

When using React and MobX, leverage React's context system to pass stores. This is particularly useful for accessing stores within the React component tree.

javascript
import React, { createContext, useContext } from 'react'; import { useLocalObservable } from 'mobx-react'; const StoreContext = createContext(null); export const StoreProvider = ({ children }) => { const store = useLocalObservable(() => ({ storeA: new StoreA(), storeB: new StoreB(), })); return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>; }; export const useStores = () => useContext(StoreContext);

In components, use the useStores hook to access storeA and storeB:

javascript
const MyComponent = () => { const { storeA, storeB } = useStores(); // Use storeA and storeB };

These methods provide ways to access stores across different stores, each with its own use cases and trade-offs. Dependency Injection via Constructor and Root Store Pattern are better suited for non-React or large React projects, while the context method is designed specifically for React. In actual projects, choose the appropriate method based on your architectural requirements and team preferences.

In MobX, there are several ways to access another store from within a store. The following are common approaches:

1. Dependency Injection via Constructor

A simple and direct method is to pass other stores as parameters when creating a store. For example:

javascript
class StoreA { constructor(storeB) { this.storeB = storeB; } } class StoreB { // StoreB methods and properties } const storeB = new StoreB(); const storeA = new StoreA(storeB);

The benefit is clear dependency declaration and ease of testing, as you can easily pass mocks or stubs.

2. Using Root Store Pattern

Typically, in larger applications, you have a "root" store that holds instances of all other child stores. This way, each child store can access other stores through the root store.

javascript
class RootStore { constructor() { this.storeA = new StoreA(this); this.storeB = new StoreB(this); } } class StoreA { constructor(rootStore) { this.rootStore = rootStore; } someMethod() { // Access StoreB's methods or properties directly through the root store return this.rootStore.storeB.someProperty; } } class StoreB { // StoreB methods and properties } const rootStore = new RootStore();

The benefit is that each store knows how to find any other store it needs without additional references or configuration.

3. Using MobX's context (in React environment)

If your application is developed with React and you're using MobX for state management, leverage React's Context API to pass stores across components.

javascript
import React, { createContext, useContext } from 'react'; const StoreContext = createContext(); const StoreProvider = ({ children, store }) => { return ( <StoreContext.Provider value={store}>{children} </StoreContext.Provider> ); }; // Custom hook to access store const useStores = () => useContext(StoreContext); // Usage in components const MyComponent = () => { const { storeA, storeB } = useStores(); // ... };

In this case, wrap your component tree with a StoreProvider at the top of your application, and access stores anywhere using the useStores custom hook.

4. Using Global Variables or Modules

Although generally not recommended, in simple applications or quick prototypes, you might choose to expose stores as global variables or export them as part of a module, as shown below:

javascript
// stores.js export const storeA = new StoreA(); export const storeB = new StoreB(storeA);

Then import them where needed:

javascript
import { storeA, storeB } from './stores'; // Use storeB's methods or properties storeA.someMethod(storeB.someProperty);

This method is simple and quick, but in large applications, it can lead to hard-to-maintain code and unclear dependencies.

The above are several ways to enable stores to access each other in MobX. Choose the appropriate method based on your application's specific requirements and structure.

2024年6月29日 12:07 回复

In MobX, there are several approaches to access another store within a store. Here are some common methods:

1. Passing via Constructor

One straightforward method is to pass another store as a parameter when instantiating a store. For example:

javascript
class StoreA { constructor(storeB) { this.storeB = storeB; } get someData() { return this.storeB.data; } } class StoreB { data = "This is data from StoreB"; } const storeB = new StoreB(); const storeA = new StoreA(storeB);

In this example, StoreA accepts an instance of StoreB during construction and stores it as a member variable, enabling access to StoreB's data within StoreA.

2. Using RootStore Pattern

Typically, in large applications, we create a RootStore that encapsulates all other stores, then reference them through this RootStore. For example:

javascript
class StoreA { constructor(rootStore) { this.rootStore = rootStore; } get otherData() { return this.rootStore.storeB.data; } } class StoreB { data = "This is data from StoreB"; } class RootStore { constructor() { this.storeB = new StoreB(); this.storeA = new StoreA(this); } } const rootStore = new RootStore();

The RootStore initializes all stores and passes itself to each store, allowing them to access one another.

3. Using Context API (React Environment)

When using MobX in a React environment, you can integrate it with React's Context API to share stores. First, create a Context:

javascript
import React from 'react'; import StoreA from './StoreA'; import StoreB from './StoreB'; const storeB = new StoreB(); const storeA = new StoreA(storeB); export const StoresContext = React.createContext({ storeA, storeB });

Then, access the stores within components using the useContext hook:

javascript
import React, { useContext } from 'react'; import { StoresContext } from './StoresContext'; const MyComponent = () => { const { storeA, storeB } = useContext(StoresContext); // Now you can use storeA and storeB within the component return ( // ... ); }

Conclusion: These are common methods for accessing other stores in MobX. Each method has specific use cases. In real-world projects, based on application complexity and personal preference, choose the most suitable method to organize your stores. This maintains code maintainability and extensibility. While providing these solutions, avoid excessive coupling to preserve store independence and reusability.

2024年6月29日 12:07 回复

你的答案