Redux / Reselect - selector reusing -


i new selectors. have created following ones:

import { createselector } 'reselect';  const getvalues = (state) => state.grid; // [3, 4, 7, 3, 2, 7, 3,...] const gettiles = (state) => state.tiles; // [0, 1, 0, 1, 0, 0, 1,...]  // counts selected tiles (ie. adds 1s) export const getselected = createselector(   [gettiles],   tiles => tiles.reduce((acc, elem) => acc + elem, 0) );  // displays values of selected tiles, rest shows 0 export const showselected = createselector(   [gettiles, getvalues],   (tiles, grid) => tiles.map((idx, i) => (idx === 1 ? grid[i] : 0)) );      export const addselected = createselector(   [showselected]   ..... );  /* export const addselected = createselector(   [showselected],   coun => coun.reduce((acc, elem) => acc + elem, 0) );  */ 

the third selector (addselected - last bottom, commented-out version) same thing first 1 (with different inputs). how can make more generic can reuse instead of writing whole 'reduce' line again?

you extract reduce part it's own function this:

import { createselector } 'reselect'  ...  // addelements adds elements given array const addelements = elements =>   elements.reduce((acc, elem) => acc + elem, 0)  export const getselected = createselector([gettiles], addelements)  export const addselected = createselector([showselected], addelements) 

i hope helpful.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -