简体   繁体   English

如何在非反应组件中连接到商店

[英]How can I connect to the store in non-react components

I would like to connect to the store in non-react components. 我想在非反应组件中连接到商店。 My main problem is that the when i try to getState or dispatch in a non-react class like this: createStoreWithApi(api).dispatch(isLoading(true)) this will create a new instance of the store, and I just want to modify the already created store. 我的主要问题是,当我尝试getState或在非反应类中dispatch ,如下所示: createStoreWithApi(api).dispatch(isLoading(true))这将创建一个新的商店实例,我只想修改已经创建的商店。 And I know that I would have to avoid having the store as a function. 而且我知道我必须避免将商店作为一种功能。

Is it possible to set the withExtraArgument after the store is created? 是否可以在创建商店后设置withExtraArgument Problem is that I cant just import the api in my store file, because I need to fetch the api from the backend first. 问题是我不能在我的商店文件中导入api ,因为我需要先从后端获取api。

This is how my store set-up looks like: 这就是我的商店设置的样子:

const createStoreWithApi = (api: IApi, initialState?: {}) => {
  const middlewares = [
    thunkMiddleware.withExtraArgument({
      api
    })
  ];
  const enhancer = composeWithDevTools(applyMiddleware(...middlewares));
  return createStore(rootReducer, initialState!, enhancer);
};

Would love some advice 会喜欢一些建议

You can create a single instance of store in a create-store.js file that holds onto the store singleton. 您可以在保存到商店单例的create-store.js文件中创建单个存储实例。 Then import that instance into all files that need access to it for store.getState and store.dispatch . 然后将该实例导入到需要访问store.getStatestore.dispatch所有文件中。

// create-store.js
let store = null;

export const createStoreWithApi = (api: IApi, initialState?: {}) => {
  const middlewares = [
    thunkMiddleware.withExtraArgument({
      api
    })
  ];
  const enhancer = composeWithDevTools(applyMiddleware(...middlewares));
  store = createStore(rootReducer, initialState!, enhancer);
};

export default store;

In your entrypoint file, you can run createStoreWithApi once with api and initialState . 在您的入口点文件中,您可以使用apiinitialState运行createStoreWithApi一次。

// index.js
import { createStoreWithApi } from "./create-store";

// init store
createStoreWithApi("api string", {...initialState});

And in all other files that need access to the store: 在需要访问商店的所有其他文件中:

import store from "./create-store";

const state = store.getState();

As far as setting the API value. 至于设置API值。 I'd recommend setting that in the store with a new reducer action. 我建议使用新的reducer动作在商店中设置它。 Then your middleware and other components will have access to it via const state = store.getState(); 然后您的中间件和其他组件将通过const state = store.getState(); . Then you don't have to worry about initializing your store with it. 然后,您不必担心使用它初始化您的商店。 And your components store.dispatch(setAPI("new api string")) then store.dispatch(isLoading(true)) . 你的组件store.dispatch(setAPI("new api string"))然后是store.dispatch(isLoading(true)) And your middleware making the calls can run store.getState() and get the current api value before making a backend call. 并且您的中间件进行调用可以运行store.getState()并在进行后端调用之前获取当前的api值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM