state-master
StateMaster improves React's methods getDerivedStateFromProps and componentDidUpdate
Installation
NPM
npm install --save state-master
Usage
; const PROP_LIST = 'width' 'height' 'bgColor' 'fontSize' 'autoSize';// or if just one propconst PROP_LIST = 'value'; // adding initial state is conditionalconst INITIAL_STATE = width: 1000 height: 500; static displayName = 'Container'; static { const nextProps prevProps state isInitial changed changedProps isChanged add addIfChanged isChangedAny addIfChangedAny isChangedAll call get } = data; // the first initial call if isInitial // adds param "name" with given value to result state ; // adds param "name" with value from nextProps to result state ; // changedProps is an array which contains all changed prop names if changedProps !== -1 ; // returns true if given prop was changed somehow if ; // returns true if given prop was changed to given value if ; // changed is true if one of props from the PROPS_LIST was changed if changed ; // returns true if some prop from the PROPS_LIST was changed // the same as "changed" if ; // returns true if some prop from given arguments (prop names) was changed if const bgColor fontSize = nextProps; ; // returns true if all props from the PROPS_LIST were changed if ; // calls "add" method if given prop was changed ; ; // calls "add" method if some prop from the PROPS_LIST was changed ; ; // returns true if all prop from given arguments (prop names) were changed if const width height = nextProps; ; // calls function with timeout // the same as setTimeout(() => this.changeSomething(), 0) // use to do some action after component updating ; // returns result state or null // it's something about debugging, put to the end console; // or you can just return state changes as usually return size: nextPropswidth + 'x' + nextPropsheight { superprops; // use "registerContext" if you need to have this context in getDerivedStateFromProps ; } { const prevProps prevState snapshot changedProps changed isChanged isChangedAny isChangedAll } = data; if const value = thisprops; this; } { // this should be done if "registerContext" was called ; } { const style size = thisstate; return <div className="container" style=style> Size is size </div> }} const Container = ;
If you have some parental component that also has getDerivedStateFromProps add one more argument
// ...const Container = ;
So the state from ParentalComponent will be added to Container's state and so on