Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138279

Confused how createSelector works

$
0
0

I'm still new to javascript and I'm having trouble understanding how state is passed to createSelector and then to the input selectors. For example, in the code below we have a selector computeDiff that uses createSelector from reselect library and an input selector myUserInputSelector.

export const myUserInputsSelector = (state) => {
  return state.userInputs;
}  

const computeDiffOfSmallestLargest = ( myUserInputs ) => {
  let largest = myUserInputs[0];
  let smallest = myUserInputs[0];

  myUserInputs.forEach( input => {
    if(input > largest) largest = input;
    if(input < smallest) smallest = input;
  });

  return largest - smallest;
}

export const computeDiff = createSelector(
  [myUserInputsSelector],
  computeDiffOfSmallestLargest
);

So to test this selector we have the following test.

describe ('computeDiffOfSmallestLargest', () => {
  it('computes the difference between largest and smallest user inputs in global array', () => {
    const state = {
      appState: 'OFF',
      screenState: {
        min: 0,
        max: 0
      },
      userInputs: [144, 44, 99]
    }

    const diff = computeDiff(state);
    expect(diff).to.be.eq(100);
  })
});

So my question is how is state argument to comuputeDiff getting to myUserInputSelector? createSelector takes an array of input selectors as first parameter and transform function as the second, not a state parameter. What am I missing here?


Viewing all articles
Browse latest Browse all 138279

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>