首页 > 解决方案 > 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;

标签: javascriptreactjsreduxreact-reduxredux-store

解决方案


选择器函数应该将存储状态作为参数,而不是捕获存储引用。举个例子:

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


推荐阅读