What are selectors in redux?

前端 未结 4 428
迷失自我
迷失自我 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:53

    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.

提交回复
热议问题