Implementing the exact same getDerivedStateFromProps over and over again to make Controllable components can be very repetitive.
make-controllable is a function to help make your components Controllable.
What is Controllable? Read this: https://medium.com/myheritage-engineering/how-controllable-react-components-maximize-reusability-86e3d233fa8e
This repetitive code:
static getDerivedStateFromProps(props, state) {
let newState = null;
if (props.value !== state.prevValue) {
newState = { prevValue: props.value };
if (props.value !== state.value) {
newState.value = props.value;
}
}
return newState;
}
Can be shorten to this one line:
static getDerivedStateFromProps(props, state) {
return makeControllable(props, state, 'value');
}
Live example here: https://codesandbox.io/s/w0mjk9z63k
npm i make-controllable
Function parameters
import makeControllable from 'make-controllable';
return makeControllable(props, state, propsMapping);
- props - the 1st argument from
getDerivedStateFromProps
- state - the 2nd argument from
getDerivedStateFromProps
- propsMapping - Object | String, mapping of prop key name to state key name. Or a String for a single prop with the same state key.
static getDerivedStateFromProps(props, state) {
return makeControllable(props, state, 'value');
}
static getDerivedStateFromProps(props, state) {
return makeControllable(props, state, {'value': 'otherValue'});
}
static getDerivedStateFromProps(props, state) {
makeControllable(props, state, {
'value': 'otherValue',
'value2': 'otherValue2',
});
}
MIT