Search code examples
javascriptredux

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;

Solution

  • Selector functions should take the store state as the argument, not capture the store reference. As an example:

    export const getSession = (state) => state.session.data || false;
    export const getToken = (state) => state.session.data.token || false;
    
    // use like:
    const session = getSession(store.getState());
    

    See my post Idiomatic Redux: Using Reselect Selectors for Encapsulation and Performance for more details.