react-component-log
Version:
HOC for easied debugging/learning of lifecycle methods for React components
157 lines (122 loc) • 6.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withComponentLogConfig = exports.withComponentLog = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _constants = require('./constants');
var _constants2 = _interopRequireDefault(_constants);
var _utils = require('./utils');
var _defaults = require('./defaults');
var _defaults2 = _interopRequireDefault(_defaults);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var devMode = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
/**
* HOC which prints info to console for easier debugging.
*
* @param Component the component which gets wrapped.
* @param config a configuration object which specifies what and how should be printed.
* @returns {WithComponentLog} the wrapped component with debugging info based on the configuration object.
*/
var withComponentLog = function withComponentLog(Component) {
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var componentName = '' + (Component.displayName || Component.name || 'Component');
var adjustedConfig = Object.assign({}, _defaults2.default, config);
var printLevel = adjustedConfig.printLevel,
shouldPrint = adjustedConfig.shouldPrint,
lifecycleMethodsConfig = _objectWithoutProperties(adjustedConfig, ['printLevel', 'shouldPrint']);
// fallback to default print level when config object has invalid console output function
var print = console[printLevel] || console[_defaults2.default.printLevel];
var createComponentLifecycleMethodMessage = (0, _utils.lifecycleMethodMessageForComponent)(componentName);
var WithComponentLog = function (_React$Component) {
_inherits(WithComponentLog, _React$Component);
function WithComponentLog() {
_classCallCheck(this, WithComponentLog);
var _this = _possibleConstructorReturn(this, (WithComponentLog.__proto__ || Object.getPrototypeOf(WithComponentLog)).call(this));
_this.printToConsole = _this.printToConsole.bind(_this);
return _this;
}
_createClass(WithComponentLog, [{
key: 'printToConsole',
value: function printToConsole(lifecycleMethod, prevProps, nextProps) {
if (devMode && lifecycleMethodsConfig[lifecycleMethod] && shouldPrint(prevProps, nextProps)) {
var message = createComponentLifecycleMethodMessage(lifecycleMethod);
if (prevProps && nextProps) {
var props = {
prevProps: prevProps,
nextProps: nextProps
};
print(message, props);
} else {
print(message);
}
}
}
}, {
key: 'componentWillMount',
value: function componentWillMount() {
this.printToConsole(_constants2.default.WILL_MOUNT);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
this.printToConsole(_constants2.default.DID_MOUNT);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.printToConsole(_constants2.default.WILL_RECEIVE_PROPS, this.props, nextProps);
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps) {
this.printToConsole(_constants2.default.SHOULD_UPDATE, this.props, nextProps);
return true;
}
}, {
key: 'componentWillUpdate',
value: function componentWillUpdate(nextProps) {
this.printToConsole(_constants2.default.WILL_UPDATE, this.props, nextProps);
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
this.printToConsole(_constants2.default.DID_UPDATE, prevProps, this.props);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.printToConsole(_constants2.default.WILL_UNMOUNT);
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(Component, this.props);
}
}]);
return WithComponentLog;
}(_react2.default.Component);
return WithComponentLog;
};
/**
* Creates a closure for the config object.
* Common usage is to avoid modifying arguments when composing multiple HOCs.
*
* @param config the config object for printing to console.
* @returns a function with the Component as input parameter
* which gets passed down to the #withComponentLog HOC by preserving the config object.
*/
var withComponentLogConfig = function withComponentLogConfig(config) {
return function (Component) {
return withComponentLog(Component, config);
};
};
// TODO 2017-07-26: README: examples of how to use both approaches
exports.withComponentLog = withComponentLog;
exports.withComponentLogConfig = withComponentLogConfig;