我有一个 NextJS React 应用程序,它使用 next-react-wrapper (基本上是一个 HOC)_app.tsx
像这样:
_app.tsx
...
import withRedux from 'next-redux-wrapper';
class Page extends App<Props> {
...
}
export default withRedux(reduxStore)(Page);
store.ts
import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import rootReducer from './reducer';
export default (
initialState: any = undefined,
) => createStore(
rootReducer,
initialState,
composeWithDevTools(applyMiddleware()),
);
我正在努力弄清楚如何访问 React 之外的存储,例如在简单的辅助函数中。我的store.ts
文件导出一个makeStore
next-redux-wrapper HOC 所需的函数(包括初始状态)。
我可以访问 React 组件中的存储并将其作为参数每次传递给我的辅助函数,但这看起来很混乱。
有没有办法直接从非 React 辅助功能模块访问存储?
这可能不是更好的选择,但是可以使用storeKey
。默认键是__NEXT_REDUX_STORE__
并使用它看起来像这样:
window.__NEXT_REDUX_STORE__.getState()
这就是发生这种情况的地方 https://github.com/kirill-konshin/next-redux-wrapper/blob/master/packages/wrapper/src/index.tsx#L34-L36
钥匙 (storeKey
) 可以在传递给 withRedux 函数参数的第二个选项参数中进行更改。对于您的实现,它看起来像这样:
export default (
initialState: any = undefined,
{ storeKey: 'whateveryouwant' } // the name on the exposed store variable
) => createStore(
rootReducer,
initialState,
composeWithDevTools(applyMiddleware()),
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)