全局 Redux 状态值 [英] Redux state value globally

查看:32
本文介绍了全局 Redux 状态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个帮助类,它提供有关登录用户的信息.我想把它作为一个静态方法并在几个地方使用它(不是反应组件)

I am having a helper class which provies info about the logged in user. I would like to have it as a static method and use it in several places ( which are not react components)

如何访问类模型中的 redux 存储值?

How to get access the redux store value in a class model ?

推荐答案

如果你不使用像 Webpack 这样的打包器,Lukas Katayama 的答案应该可以工作 window.store = createStore(...)但如果您正在使用它,您也可以通过导出和导入您需要的地方来公开您的 redux 存储.

If you're not using a bundler like Webpack, Lukas Katayama's answer should work window.store = createStore(...) but if you are using one, you can also expose your redux store by exporting it and importing where you need it.

//store.js
export const store = createStore(...);

//other file
import { store } from './store.js';

const someFuncOrClass = (...) => {
    const user = store.getState().userInfo;
}

这篇关于全局 Redux 状态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆