In-depth checking props in runtime for any React app.
yarn add -D react-props-monitor
or
npm install --save-dev react-props-monitor
import React from 'react';
import initPropsMonitor, { PropsMonitor } from 'react-props-monitor';
initPropsMonitor(React);
/../
render() {
return (
<div>
<Root />
<PropsMonitor />
</div>
);
}
ctrl+i to open a monitor.
PropsMonitor displays exactly which props caused the error based on PropTypes of component.
You can define any validation function for props, based on prevProps, nextProps and name of component.
const costIsVerySmallNumber = ({ nextProps }) => {
if (nextProps.value < 2000000)
return 'Caution your cost prop is small a number.';
return false;
};
const costShouldIncrease = ({ prevProps, nextProps, name }) => {
if (
name === 'TextBox' &&
prevProps &&
prevProps.cost > nextProps.cost
) {
return 'Hey dude, I think you must to increase your cost.';
}
return false;
};
const validationFns = [
costIsVerySmallNumber,
costShouldIncrease,
];
/../
render() {
return (
<div>
<Root />
<PropsMonitor validation={validationFns} />
</div>
);
}
For a list of components you can add grouping
const groupExtraComponents = ({ name }) =>
/^Extra/.test(name)
? `Extra`
: null;
const groupsFns = [
groupExtraComponents,
];
/../
render() {
return (
<div>
<Root />
<PropsMonitor groups={groupsFns} />
</div>
);
}
- define and lock props
- forecast for types based on real props in runtime