问题
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