Skip to content

Commit 159bcca

Browse files
author
tomas.jilek
committed
first version
1 parent 0a278d0 commit 159bcca

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
node_modules
3+
pkg/

package-lock.json

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@morosystems/react-debug",
3+
"version": "0.0.1",
4+
"description": "Debug utils for react components.",
5+
"sideEffects": false,
6+
"scripts": {},
7+
"keywords": [
8+
"react",
9+
"debug",
10+
"hoc"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/morosystems/react-debug.git"
15+
},
16+
"author": "Morosystems <info@morosystems.cz>",
17+
"license": "MIT",
18+
"main": "src/index.js",
19+
"devDependencies": {
20+
"react": "16.8.4"
21+
},
22+
"peerDependencies": {
23+
"react": ">15"
24+
},
25+
"@pika/pack": {
26+
"pipeline": [
27+
[
28+
"@pika/plugin-standard-pkg"
29+
],
30+
[
31+
"@pika/plugin-build-node"
32+
],
33+
[
34+
"@pika/plugin-build-web"
35+
]
36+
]
37+
}
38+
}

src/debug.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
3+
const defaultMount = (nameFn) => (props) => `${nameFn(props)} mounts`;
4+
const defaultUnmount = (nameFn) => (props) => `${nameFn(props)} unmounts`;
5+
const defaultUpdate = (nameFn) => (props, nextProps) => {
6+
const changed = Object.keys(nextProps).filter((key) => props[key] !== nextProps[key]).join(', ');
7+
return `${nameFn(nextProps)} updates ${changed}`;
8+
};
9+
const logIfExists = (string) => {
10+
if (string) {
11+
console.log(string);
12+
}
13+
};
14+
15+
/**
16+
* [HoC] Debug component. Logs debug information about component lifecycle: componentDidMount, componentWillUnmount, componentWillUpdate.
17+
*
18+
* @param {?object} $0 Configuration (optional)
19+
* @param {?string|function} $0.name Name of the component. May be a string or function from props. If undefined, displayName is used.
20+
* @param {?function} $0.mount Generates componentDidMount message. Receives props as argument.
21+
* @param {?function} $0.unmount Generates componentWillUnmount message. Receives props as argument.
22+
* @param {?function} $0.update Generates componentWillUpdate message. Receives props and nextProps as arguments.
23+
* @return {component => component} Configured debug component wrapper.
24+
*/
25+
const debug = ({name, mount, unmount, update} = {}) => (Component) => {
26+
const finalName = name || Component.displayName || Component.name || 'Component';
27+
const nameFn = typeof finalName === 'function' ? finalName : () => finalName;
28+
const onMount = mount || defaultMount(nameFn);
29+
const onUnmount = unmount || defaultUnmount(nameFn);
30+
const onUpdate = update || defaultUpdate(nameFn);
31+
32+
return class extends React.Component {
33+
componentDidMount() {
34+
logIfExists(onMount(this.props));
35+
}
36+
37+
componentWillUpdate(nextProps) {
38+
logIfExists(onUpdate(this.props, nextProps));
39+
}
40+
41+
componentWillUnmount() {
42+
logIfExists(onUnmount(this.props));
43+
}
44+
45+
render() {
46+
return <Component {...this.props} />;
47+
}
48+
};
49+
};
50+
export default debug;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as debug} from "./debug";

0 commit comments

Comments
 (0)