redux reselect is returning a function

淺唱寂寞╮ 提交于 2020-04-18 12:51:32

问题


For some strange weird reason my reselect selector is returning this function :

ƒ () {
    if (!areArgumentsShallowlyEqual(equalityCheck, lastArgs, arguments)) {
        // apply arguments instead of spreading for performance.
        lastResult = func.apply(null, arguments);
    }

I do not understand why it is doing this. It is supposed to return some JSON which I can use to map in my mapToProps.

My redux mapping is simple enough and looks like this :

const mapStateToProps = (state) => {
    return {
        initialValues: selectShop(state),
    }
}

My selector file looks like this :

import { createSelector } from 'reselect';

//shops selector
const selectDomain = () => state => (state ? state.shops : {});

export const selectShop = () =>
    createSelector(
        selectDomain(),
        shops => {
            let shop = shops.filter(shop => shop.selected);
            return shop[0];
        },
    );

Why am I getting a function returned instead of JSON?


回答1:


You are using createSelector wrong. It actually returns a memoized version of a function.

Something like this should work

import { createSelector } from 'reselect';

//shops selector
const selectDomain = state => (state ? state.shops : []); // I've fixed it to return an array 

export const selectShop = createSelector(
  selectDomain,
  // you could use find instead of `filter` herr
  shops => shops.find(shop => shop.selected)
)



回答2:


Also to add to the accepted answer. The selector is being called wrong. This is how it should be called correctly :

const mapStateToProps = (state) => {
    let selector = selectShop()

    return {
        initialValues: selector (state),
    }
}


来源:https://stackoverflow.com/questions/55147272/redux-reselect-is-returning-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!