Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux reselect is returning a function

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?

like image 966
Oliver Watkins Avatar asked Sep 12 '26 16:09

Oliver Watkins


2 Answers

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)
)
like image 88
Yury Tarabanko Avatar answered Sep 16 '26 02:09

Yury Tarabanko


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),
    }
}
like image 23
Oliver Watkins Avatar answered Sep 16 '26 02:09

Oliver Watkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!