What are selectors in redux?

ReactjsReduxRedux Saga

Reactjs Problem Overview


I am trying to follow this code in redux-saga

export const getUser = (state, login) => state.entities.users[login]
export const getRepo = (state, fullName) => state.entities.repos[fullName]

Which is then used in the saga like this:

import { getUser } from '../reducers/selectors'

// load user unless it is cached
function* loadUser(login, requiredFields) {
  const user = yield select(getUser, login)
  if (!user || requiredFields.some(key => !user.hasOwnProperty(key))) {
    yield call(fetchUser, login)
  }
}

This getUser reducer (is it even a reducer) looks very different from what I would normally expect a reducer to look like.

Can anyone explain what a selector is and how getUser is a reducer and how it fits in with redux-saga?

Reactjs Solutions


Solution 1 - Reactjs

getUser is not a reducer, it is indeed a selector, that is, a function that knows how to extract a specific piece of data from the store.

Selectors provide an additional layer such that if you altered your store structure and all of a sudden your users were no longer at state.entities.users but instead at state.users.objects.entities (or whatever) then you only need to update the getUser selector and not every place in your app where you were making a reference to the old location.

That makes them particularly handy when it comes to refactoring your Redux store.

Solution 2 - Reactjs

Selectors are getters for the redux state. Like getters, selectors encapsulate the structure of the state, and are reusable. Selectors can also compute derived properties.

You can write selectors, such as the ones you saw in redux-saga. For example:

const getUsersNumber = ({ users }) => users.length;

const getUsersIds = ({ users }) => users.map(({ id }) => id);

etc...

You can also use reselect, which is a simple “selector” library for Redux, that memoize selectors to make them more efficient.

Solution 3 - Reactjs

Selectors are functions that take Redux state as an argument and return some data to pass to the component.

const getUserData = state => state.user.data;

Why should it be used?

  1. One of the main reasons is to avoid duplicated data in Redux.
  2. Your data object shape keeps varying as your application grows, so rather than making changes in all the related component.It is much recommended/easier to change the data at one place.
  3. Selectors should be near reducers because they operate on the same state. It is easier for data to keep in sync.

Using reselect helps to memoize data meaning when the same input is passed to the function, returns the previous result rather than recalculating again.So, this enhances your application performance.

Solution 4 - Reactjs

function mapStateToProps (state) {
    return {
        user: state.user,
    }
}

initialState of reducer by user store
const initialState = {
  isAdmin:false,
  isAuth:false,
  access:[1,2,5]
};

class AppComp extends React.Component{
render(){
        const {user: { access:access}} = this.props;
        const rand = Math.floor(Math.random()*4000)
        return (<div>
            {`APP ${rand} `}
    <input type="button" defaultValue="change auth" onClick={this.onChangeUserAuth} />
        <p>TOTAL STATUS COUNT IS {access.length}</p>
        </div>)
    }
}}

but you can use selector

var getUser = function(state) {
    return state.user
}


const getAuthProp = createSelector(
    getUser,
    (user) => user.access
);


function mapStateToProps (state) {
    return {
       // user: state.user,
        access: getAuthProp(state)
    }
}

Main Problem is this component use all user: state.user and any changes in user (etc isAdmin ,isAuth, access) runs rerender this component which need only part of this store - access!!!

> In Redux, whenever an action is called anywhere in the application, > all mounted & connected components call their mapStateToProps > function. This is why Reselect is awesome. It will just return the > memoized result if nothing has changed. > > > In the real world, you will most likely need the same certain part of > your state object in multiple components.

https://medium.com/@parkerdan/react-reselect-and-redux-b34017f8194c

The createSelector function provided by Reselect implements the most basic way to derive a selector from previous selectors. The simplest use case is to derive a selector from a single other selector. In this case, the parameters to createSelector are the input selector and a function transforming the result of that selector into the result of the new selector. For example

var getProducts = function(state) {
    return state.products
}


import {getProducts} from '../app/selectors'
import {createSelector} from 'reselect'

export const getProductTitles = createSelector(
    getProducts,
    (products) => products.map((product) => product.get('title'))
)

This is equivalent to (ignoring memoization):

import {getProducts} from '../app/selectors'

export const getProductTitles = (state) => {
    return getProducts(state).map((product) => product.get('title'))
}

The createSelector function can combine data from multiple selectors as well as from a single selector. We can pass any number of selectors to createSelector, and their results will be passed to the function passed as the final argument. For a (somewhat contrived) example:

const isInCheckout = createSelector(
    getIsShippingPage,
    getIsBillingPage,
    getIsConfirmationPage,
    (isShipping, isBilling, isConfirmation) =>
        isShipping || isBilling || isConfirmation
)

is equivalent to

const isInCheckout = (state) => {
    return (
        getIsShippingPage(state) ||
        getIsBilingPage(state) ||
        getIsConfirmationPage(state)
    )
}

common pattern when writing mapStateToProps functions with selectors is to return an object with each key storing the result of a particular selector. The createStructuredSelector helper function in Reselect lets us write this pattern with the minimum of boilerplate. For example, if we writ

const mapStateToProps = createStructuredSelector({
    title: getProductTitle,
    price: getProductPrice,
    image: getProductImage
})

it is equivalent to

const mapStateToProps = (state) => {
    return {
        title: getProductTitle(state),
        price: getProductPrice(state),
        image: getProductImage(state)
    }
}

https://docs.mobify.com/progressive-web/0.15.0/guides/reselect/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondagda1View Question on Stackoverflow
Solution 1 - ReactjshorydView Answer on Stackoverflow
Solution 2 - ReactjsOri DroriView Answer on Stackoverflow
Solution 3 - ReactjsCodeZombieView Answer on Stackoverflow
Solution 4 - ReactjszloctbView Answer on Stackoverflow