Skip to content

Commit

Permalink
⬆️ react / lifecycle (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Oct 16, 2018
1 parent 2505d5c commit 379c6b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
59 changes: 29 additions & 30 deletions packages/core/build/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// eslint-disable-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -100,33 +100,29 @@ var _default = function _default() {
function (_Component) {
_inherits(_class, _Component);

function _class(_props, context) {
var _this;

_classCallCheck(this, _class);

_this = _possibleConstructorReturn(this, _getPrototypeOf(_class).call(this, _props, context));

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "omitLoadInProps", function (props) {
_createClass(_class, null, [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, state) {
var isLoadAFunction = isFunction(props[loadFunctionName]);

if (isLoadAFunction) {
_this.setState({
return _objectSpread({}, state, {
props: _objectSpread({}, props, _defineProperty({}, loadFunctionName, undefined))
});
} else {
_this.setState({
props: props
});
}

return isLoadAFunction;
});
return _objectSpread({}, state, {
props: props
});
}
}]);

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "componentWillReceiveProps", function (nextProps) {
_this.omitLoadInProps(nextProps);
});
function _class(props, context) {
var _this;

_classCallCheck(this, _class);

_this = _possibleConstructorReturn(this, _getPrototypeOf(_class).call(this, props, context));
_this.state = {
props: {},
print: true
Expand All @@ -135,8 +131,8 @@ var _default = function _default() {
}

_createClass(_class, [{
key: "componentWillMount",
value: function componentWillMount() {
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;

// Load from hoc argument
Expand All @@ -145,9 +141,10 @@ var _default = function _default() {
} // Load from props


if (this.omitLoadInProps(this.props)) {
// eslint-disable-next-line react/destructuring-assignment
this.props[loadFunctionName](this.props, this.context);
var loadFunction = this.props[loadFunctionName]; // eslint-disable-line react/destructuring-assignment

if (isFunction(loadFunction)) {
loadFunction(this.props, this.context);
} // set delay


Expand Down Expand Up @@ -179,6 +176,7 @@ var _default = function _default() {
var props = this.state.props;

if (isInError(this.props, this.state, this.context)) {
// react renders nothing if you return null but is not happy if you return undefined
return ErrorIndicator === undefined ? null : _react.default.createElement(ErrorIndicator, props);
}

Expand All @@ -189,7 +187,8 @@ var _default = function _default() {
if (!this.state.print) {
// eslint-disable-line react/destructuring-assignment
return null;
}
} // react renders nothing if you return null but is not happy if you return undefined


return LoadingIndicator === undefined ? null : _react.default.createElement(LoadingIndicator, props);
}
Expand Down
45 changes: 20 additions & 25 deletions packages/core/src/core.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ export default (
return class extends Component {
static displayName = displayName

static getDerivedStateFromProps(props, state) {
const isLoadAFunction = isFunction(props[loadFunctionName])

if (isLoadAFunction) {
return {
...state,
props: {
...props,
[loadFunctionName]: undefined,
},
}
}

return { ...state, props }
}

constructor(props, context) {
super(props, context)

Expand All @@ -68,16 +84,16 @@ export default (
}
}

componentWillMount() {
componentDidMount() {
// Load from hoc argument
if (isLoadFunction) {
load(this.props, this.context)
}

// Load from props
if (this.omitLoadInProps(this.props)) {
// eslint-disable-next-line react/destructuring-assignment
this.props[loadFunctionName](this.props, this.context)
const loadFunction = this.props[loadFunctionName] // eslint-disable-line react/destructuring-assignment
if (isFunction(loadFunction)) {
loadFunction(this.props, this.context)
}

// set delay
Expand All @@ -93,27 +109,6 @@ export default (
}
}

omitLoadInProps = (props) => {
const isLoadAFunction = isFunction(props[loadFunctionName])

if (isLoadAFunction) {
this.setState({
props: {
...props,
[loadFunctionName]: undefined,
},
})
} else {
this.setState({ props })
}

return isLoadAFunction
}

componentWillReceiveProps = (nextProps) => {
this.omitLoadInProps(nextProps)
}

render() {
const { props } = this.state
if (isInError(this.props, this.state, this.context)) {
Expand Down

0 comments on commit 379c6b8

Please sign in to comment.