我有以下代码用于我的redux存储,并且当与反应连接时,一切正常。现在我试图在动作调度程序(反应之外)中访问存储的当前状态,但是当我在初始调度后使用它时,我得到空值。
applicationVariants.all { variant ->
println('variantApp: ' + variant.getName())
def versionCode = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
def versionName = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
variant.outputs.all { output ->
output.versionCodeOverride = versionCode
output.versionNameOverride = versionName
}
}
我的商店:
import configureStore from '../../common/store/configureStore';
const history = createHistory();
const reduxRouterMiddleware = syncHistory(history);
// Create a new Redux store instance
const store = configureStore({},reduxRouterMiddleware);
console.log("Store data ===>"+JSON.stringify(store.getState()));
更新1:
我对store变量进行了一些修改,使store成为全局变量,并添加了一个函数来返回store,这似乎有效。这是正确的方法吗?
import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import DevTools from '../containers/DevTools';
import createLogger from 'redux-logger';
import promise from 'redux-promise';
export default function configureStore(initialState, reduxRouterMiddleware) {
const logger = createLogger();
const createStoreWithMiddleware = applyMiddleware(
thunk,promise,
reduxRouterMiddleware
)(createStore);
const isProduction = process.env.NODE_ENV === 'production';
let store;
if (!isProduction) {
store = createStoreWithMiddleware(rootReducer, initialState, compose(
DevTools.instrument()
));
}
else if(initialState!=''){
store = createStoreWithMiddleware(rootReducer, initialState, compose(
));
}else{
store = createStore(rootReducer)
}
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers');
store.replaceReducer(nextRootReducer)
})
}
return store
}
我可以按如下方式访问商店。
import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import DevTools from '../containers/DevTools';
import createLogger from 'redux-logger';
import promise from 'redux-promise';
let store;
export default function configureStore(initialState, reduxRouterMiddleware) {
const logger = createLogger();
const createStoreWithMiddleware = applyMiddleware(
thunk,promise,
reduxRouterMiddleware
)(createStore);
const isProduction = process.env.NODE_ENV === 'production';
if (!isProduction && initialState!='') {
store = createStoreWithMiddleware(rootReducer, initialState, compose(
DevTools.instrument()
));
}
else if(initialState!=''){
store = createStoreWithMiddleware(rootReducer, initialState, compose(
));
}
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers');
store.replaceReducer(nextRootReducer)
})
}
return store
}
export function returnStore() {
return store
}
答案 0 :(得分:2)
由于您使用的是redux-thunk
,因此您可以从任何动作创建者获取当前的商店状态,如下所示:
function someAction() {
return (dispatch, getState) => {
console.log(store.getState())
}
}
我的习惯是你使用它而不是全局公开商店。
答案 1 :(得分:0)
这将是一个评论,但由于我没有足够的声誉,让我做一些答案。
首先,我是一个像你一样的学习者,而不是JS,React或Redux的专家。只是出于好奇,你究竟需要在哪里存储?以下是我在哪里以及如何获得商店的地方:
当然你可以像你写的那样得到商店。实际上,我不太确定,但我认为对于redux-thunk,我们使用的是类似的概念。在动作创建者功能(不是动作创建者本身)中,我们从商店接收dispatch和getState。所以,它会以某种方式从商店返回这些方法。