我们有一个ReactNative应用程序,它使用redux,redux-persist和HeadlessJS任务.此任务需要访问商店.由于任务在没有启动整个应用程序的情况下触发(因此默认情况下没有访问权限),我们认为我们可以简单地在任务中创建商店,以便它可以通过redux-persist重新水合.然而,事实证明,以这种方式创建的商店与应用程序中的商店不同:运行后,它们包含不同的值.我们用几种方式对它进行了测试,看起来确实是商店的问题(而不是例如动作)我们应该如何从HeadlessJS任务访问Redux商店?
相关代码
store/configure.js:
configureStore = (client) => {
const middleware = createMiddleware(client);
const finalCreateStore = applyMiddleware(thunk, middleware, logger)(createStore);
const store = finalCreateStore(rootReducer, undefined, autoRehydrate());
return store;
};
Run Code Online (Sandbox Code Playgroud)
在使用中(在应用程序和服务中):
const client = new ApiClient();
const store = configureStore(client);
client.setStore(store);
persistStore(store, {
storage: AsyncStorage,
}
Run Code Online (Sandbox Code Playgroud)
在应用程序中,我们只使用react-reactx中的Provider来使用store,在我们使用store.dispatch的服务中.