This repo is no longer maintained. Component was moved to internal repo react-boilerplate.
Debug utils for react components. Intended for development only, not for production
npm install -D @morosystems/react-debug
Lets you see in console when component mounts, unmounts, updates and why it updated:
import {debug} from "@morosystems/react-debug";
export default debug()(MyComponent);
This logs roughly the following information:
MyComponent mounts
MyComponent updates name,input
MyComponent updates error
MyComponent unmounts
Displayed information can be changed using the configuration object, e.g.:
export default debug({name: 'Test'})(MyComponent);
In order to not importing component all the time, you can import @morosystems/react-debug/lib/patch
just once, in your main file,
then use debug()
directly from global namespace. You can also safely forget about removing this import for production,
since it will inject debug()
only when NODE_ENV is development
import "@morosystems/react-debug/lib/patch"; // eslint-disable-line import/no-extraneous-dependencies, removed in production
...
export default debug()(MyComponent);
You can specify name that is logged using the name
property. You can specify:
- String.
- Function of props. This is useful when debugging one specific instance of generic component, e.g.
Field
.
export default debug({name: (props) => props.input.name})(Field);
You can also use custom messages for each lifecycle event.
mount
as a function from props to string.update
as a function from props and next props to string.unmount
as a function from props to string.
You can also supress loging by returning null
, undefined
or false
:
const update = (props, nextProps) => (props.name !== nextProps.name) && `${props.name} updates to ${nextProps.name}`;
export default debug({update})(MyComponent);