Skip to content

Commit

Permalink
feat: 🎸 improve <Lifecycles> component
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 22, 2018
1 parent 961bfd6 commit baae6ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/Lifecycles/__story__/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ storiesOf('Inversion/Lifecycles', module)
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Lifecycles.md')}))
.add('Example', () =>
<Lifecycles
foo='bar'
willMount={action('willMount')}
didMount={action('didMount')}
willUnmount={action('willUnmount')}
>
Expand Down
36 changes: 12 additions & 24 deletions src/Lifecycles/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
import {Component} from 'react';
import {render} from 'react-universal-interface';
import {noop} from '../util';

export interface ILifecyclesProps {
[key: string]: any;
willMount?: (props) => void;
didMount?: (props) => void;
willReceiveProps?: (nextProps, props) => void;
shouldUpdate?: (nextProps, props) => boolean;
willUpdate?: (nextProps, props) => void;
didUpdate?: (props, prevProps) => void;
getSnapshotBeforeUpdate?: (prevProps, props) => any;
didUpdate?: (props, prevProps, snapshot) => void;
willUnmount?: (props) => void;
didCatch?: (error, info, props) => void;
[key: string]: any;
}

export class Lifecycles extends Component<ILifecyclesProps, {}> {
static defaultProp = {
willMount: noop,
static defaultProps = {
didMount: noop,
willReceiveProps: noop,
shouldUpdate: noop,
willUpdate: noop,
getSnapshotBeforeUpdate: noop,
didUpdate: noop,
willUnmount: noop,
didCatch: noop,
};

componentWillMount () {
return this.props.willMount(this.props);
}

componentDidMount () {
return this.props.didMount(this.props);
}

componentWillReceiveProps (nextProps) {
return this.props.willReceiveProps(nextProps, this.props);
}

shouldComponentUpdate (nextProps) {
return this.props.shouldUpdate(nextProps, this.props);
const fn = this.props.shouldUpdate;
return fn ? fn(nextProps, this.props) : true;
}

componentWillUpdate (nextProps) {
return this.props.willUpdate(nextProps, this.props);
getSnapshotBeforeUpdate(prevProps) {
return this.props.getSnapshotBeforeUpdate(prevProps, this.props);
}

componentDidUpdate (prevProps) {
return this.props.didUpdate(this.props, prevProps);
componentDidUpdate (prevProps, prevState, snapshot) {
return this.props.didUpdate(this.props, prevProps, snapshot);
}

componentWillUnmount () {
Expand All @@ -59,6 +47,6 @@ export class Lifecycles extends Component<ILifecyclesProps, {}> {
}

render () {
return render(this.props, null);
return this.props.children;
}
}

1 comment on commit baae6ea

@streamich
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build version: 2.8.1-lifecycles.145 🤞 lifecycles on Travis 🎉

Please sign in to comment.