What are selectors in redux?

前端 未结 4 427
迷失自我
迷失自我 2020-12-23 16:22

I am trying to follow this code in redux-saga

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


        
4条回答
  •  死守一世寂寞
    2020-12-23 16:48

    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.

提交回复
热议问题