Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Created January 5, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsnell/c0403125f9e32e79b5c4d6396fb2550f to your computer and use it in GitHub Desktop.
Save dmsnell/c0403125f9e32e79b5c4d6396fb2550f to your computer and use it in GitHub Desktop.
Building independent reducers of inter-related data
const height = ( state = 8, { type, height } ) =>
'SET_HEIGHT' === type || 'RESET_BOARD' === type
? height
: state;
const width = ( state = 8, { type, width } ) =>
'SET_WIDTH' === type || 'RESET_BOARD' === type
? width
: state;
const grid = ( state = [], { type, height, width } ) => {
if ( 'SET_HEIGHT' === type ) {
return state.slice( 0, height );
}
if ( 'SET_WIDTH' === type ) {
return state.map( row => row.slice( 0, width ) );
}
if ( 'RESET_BOARD' === type ) {
return ( new Array( height ) ).map( row => new Array( width ) );
}
return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment