redux reselect 正在返回一个函数

redux reselect is returning a function

由于一些奇怪的奇怪原因,我的重新选择选择器return正在使用此功能:

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

我不明白为什么要这样做。它应该 return 一些 JSON 我可以用它来映射到我的 mapToProps.

我的 redux 映射很简单,看起来像这样:

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

我的选择器文件如下所示:

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];
        },
    );

为什么我得到的是函数 returned 而不是 JSON?

您使用的 createSelector 有误。它实际上 returns 一个函数的记忆版本。

像这样的东西应该可以工作

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)
)

还要添加到已接受的答案中。选择器被错误调用。这才是正确的调用方式:

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

    return {
        initialValues: selector (state),
    }
}