Skip to content

Commit baae6ea

Browse files
committed
feat: 🎸 improve <Lifecycles> component
1 parent 961bfd6 commit baae6ea

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

‎src/Lifecycles/__story__/story.tsx‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ storiesOf('Inversion/Lifecycles', module)
88
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Lifecycles.md')}))
99
.add('Example', () =>
1010
<Lifecycles
11-
foo='bar'
12-
willMount={action('willMount')}
1311
didMount={action('didMount')}
1412
willUnmount={action('willUnmount')}
1513
>

‎src/Lifecycles/index.ts‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
11
import {Component} from 'react';
2-
import {render} from 'react-universal-interface';
32
import {noop} from '../util';
43

54
export interface ILifecyclesProps {
6-
[key: string]: any;
7-
willMount?: (props) => void;
85
didMount?: (props) => void;
9-
willReceiveProps?: (nextProps, props) => void;
106
shouldUpdate?: (nextProps, props) => boolean;
11-
willUpdate?: (nextProps, props) => void;
12-
didUpdate?: (props, prevProps) => void;
7+
getSnapshotBeforeUpdate?: (prevProps, props) => any;
8+
didUpdate?: (props, prevProps, snapshot) => void;
139
willUnmount?: (props) => void;
1410
didCatch?: (error, info, props) => void;
11+
[key: string]: any;
1512
}
1613

1714
export class Lifecycles extends Component<ILifecyclesProps, {}> {
18-
static defaultProp = {
19-
willMount: noop,
15+
static defaultProps = {
2016
didMount: noop,
21-
willReceiveProps: noop,
2217
shouldUpdate: noop,
23-
willUpdate: noop,
18+
getSnapshotBeforeUpdate: noop,
2419
didUpdate: noop,
2520
willUnmount: noop,
2621
didCatch: noop,
2722
};
2823

29-
componentWillMount () {
30-
return this.props.willMount(this.props);
31-
}
32-
3324
componentDidMount () {
3425
return this.props.didMount(this.props);
3526
}
3627

37-
componentWillReceiveProps (nextProps) {
38-
return this.props.willReceiveProps(nextProps, this.props);
39-
}
40-
4128
shouldComponentUpdate (nextProps) {
42-
return this.props.shouldUpdate(nextProps, this.props);
29+
const fn = this.props.shouldUpdate;
30+
return fn ? fn(nextProps, this.props) : true;
4331
}
4432

45-
componentWillUpdate (nextProps) {
46-
return this.props.willUpdate(nextProps, this.props);
33+
getSnapshotBeforeUpdate(prevProps) {
34+
return this.props.getSnapshotBeforeUpdate(prevProps, this.props);
4735
}
4836

49-
componentDidUpdate (prevProps) {
50-
return this.props.didUpdate(this.props, prevProps);
37+
componentDidUpdate (prevProps, prevState, snapshot) {
38+
return this.props.didUpdate(this.props, prevProps, snapshot);
5139
}
5240

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

6149
render () {
62-
return render(this.props, null);
50+
return this.props.children;
6351
}
6452
}

0 commit comments

Comments
 (0)