javascript - Extract Redux Store/State as Selector
问题描述
Currently I'm having to reconfigure my store to create selector. Is there a better way to do this.
import { configureStore } from "../configureStore";
const { store } = configureStore();
export const getSession = () => store.getState().session.data || false;
export const getToken = () => store.getState().session.data.token || false;
解决方案
选择器函数应该将存储状态作为参数,而不是捕获存储引用。举个例子:
export const getSession = (state) => state.session.data || false;
export const getToken = (state) => state.session.data.token || false;
// use like:
const session = getSession(store.getState());
有关更多详细信息,请参阅我的帖子Idiomatic Redux: Using Reselect Selectors for Encapsulation and Performance。
推荐阅读
- python - 如何在另一台服务器上的另一个 virtualenv 环境中运行 django 文件?
- html - 在上述列具有偏移量的 boostrap 行下方对齐内容
- javascript - 支付成功后如何调用函数?在角
- ios - NotificationSendingSplitViewcontroller,它是干什么用的?
- python - 优化此功能--numpy广播问题
- php - 我想在 php 中回显所有具有相同用户 ID 的类名
- docusignapi - 嵌入式签名欧盟合格 IDnow TSP:更改签名者的电子邮件地址而无需重新识别?
- java - 使用 Spring MVC 无法同时正确打印 ArrayList 和 HTML 中的表单
- kubernetes - helm 无法在 AKS 中列出版本
- model - 仅包含几何图形的 3D 模型文件格式