Skip to content

Instantly share code, notes, and snippets.

@HyphnKnight
Last active November 1, 2016 16:56
Show Gist options
  • Select an option

  • Save HyphnKnight/b27ed0e01b5039cb49800bea2ecc6738 to your computer and use it in GitHub Desktop.

Select an option

Save HyphnKnight/b27ed0e01b5039cb49800bea2ecc6738 to your computer and use it in GitHub Desktop.
import React from 'react-lite';
import {
isUndefined,
isString,
isBoolean,
isEqual,
objectFind
} from './functional';
class Reflex extends React.Component {
constructor() {
super();
this.state = {};
}
shouldComponentUpdate( nextProps, nextState ) {
if ( !!objectFind( nextState, ( stateValue, stateName ) => isUndefined( this.state[stateName] ) ) ) {
throw 'Reflex - Attempting to set value of unregistered key.'
}
return !!objectFind( nextProps, ( propValue, propName ) => !isEqual( this.props[propName], propsValue ) ) ||
!!objectFind( nextState, ( stateValue, stateName ) => !isEqual( this.state[stateName], stateValue ) );
}
generateClasses( ...propertyNames ) {
let cssClasses = '';
propertyNames.forEach( name => {
if ( !isUndefined( this.state[ name ]) ) {
if ( isBoolean( this.state[ name ] ) ) {
if ( !!this.state[ name ] ) {
cssClasses += ` state-${name}`;
}
} else if ( isString( this.state[ name ] ) ) {
cssClasses += ` state-${name}-${this.state[ name ]}`;
}
} else {
throw 'Reflex - Attempting to create class from unregistered key';
}
} );
return cssClasses;
}
}
export default Reflex;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment