Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit 28b3021

Browse files
committed
➕ with-lifecycle: add onGetSnapshotBeforeUpdate and tweak onDidUpdate
1 parent dcfc69b commit 28b3021

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

packages/with-lifecycle/readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Inspired by [Reassemble](https://github.com/wikiwi/reassemble), in comparison wi
99
* `onConstructor(props)`
1010
* `onWillMount(props)`
1111
* `onDidMount(props)`
12-
* `onReceiveProps(props, nextProps)`
13-
* `onWillUpdate(props, nextProps)`
14-
* `onDidUpdate(prevProps, props)`
12+
* `onReceiveProps(props, nextProps)``getDerivedStateFromProps` under the hood but without internal state
13+
* `onGetSnapshotBeforeUpdate(prevProps, props)` – any returned value will be passed as `snapshot` parameter to `onDidUpdate`
14+
* `onDidUpdate(prevProps, props, snapshot)`
1515
* `onWillUnmount(props)`
1616
* `onDidCatch(error, info)`
1717

packages/with-lifecycle/src/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ export default (methodsArg) => (Target) => {
3535
}
3636
}
3737

38+
if (methods.onGetSnapshotBeforeUpdate) {
39+
this.getSnapshotBeforeUpdate = (prevProps) => {
40+
return methods.onGetSnapshotBeforeUpdate(prevProps, this.props)
41+
}
42+
}
43+
3844
if (methods.onDidUpdate) {
39-
this.componentDidUpdate = (prevProps) => {
40-
methods.onDidUpdate(prevProps, this.props)
45+
this.componentDidUpdate = (prevProps, _, snapshot) => {
46+
methods.onDidUpdate(prevProps, this.props, snapshot)
4147
}
4248
}
4349

packages/with-lifecycle/test/__snapshots__/index.js.snap

+68
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ Array [
6464
"b": 2,
6565
"c": 3,
6666
},
67+
undefined,
68+
],
69+
]
70+
`;
71+
72+
exports[`withLifecycle as factory onGetSnapshotBeforeUpdate 1`] = `
73+
Array [
74+
Array [
75+
Object {
76+
"a": 1,
77+
"b": 2,
78+
},
79+
Object {
80+
"a": 1,
81+
"b": 2,
82+
"c": 3,
83+
},
84+
],
85+
]
86+
`;
87+
88+
exports[`withLifecycle as factory onGetSnapshotBeforeUpdate 2`] = `
89+
Array [
90+
Array [
91+
Object {
92+
"a": 1,
93+
"b": 2,
94+
},
95+
Object {
96+
"a": 1,
97+
"b": 2,
98+
"c": 3,
99+
},
100+
"snapshot",
67101
],
68102
]
69103
`;
@@ -191,6 +225,40 @@ Array [
191225
"b": 2,
192226
"c": 3,
193227
},
228+
undefined,
229+
],
230+
]
231+
`;
232+
233+
exports[`withLifecycle as object onGetSnapshotBeforeUpdate 1`] = `
234+
Array [
235+
Array [
236+
Object {
237+
"a": 1,
238+
"b": 2,
239+
},
240+
Object {
241+
"a": 1,
242+
"b": 2,
243+
"c": 3,
244+
},
245+
],
246+
]
247+
`;
248+
249+
exports[`withLifecycle as object onGetSnapshotBeforeUpdate 2`] = `
250+
Array [
251+
Array [
252+
Object {
253+
"a": 1,
254+
"b": 2,
255+
},
256+
Object {
257+
"a": 1,
258+
"b": 2,
259+
"c": 3,
260+
},
261+
"snapshot",
194262
],
195263
]
196264
`;

packages/with-lifecycle/test/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ describe('withLifecycle', () => {
6161
expect(mockOnReceiveProps.mock.calls).toMatchSnapshot()
6262
})
6363

64+
it('onGetSnapshotBeforeUpdate', () => {
65+
const mockOnGetSnapshotBeforeUpdate = jest.fn(() => 'snapshot')
66+
const mockOnDidUpdate = jest.fn()
67+
const EnhancedTarget = withLifecycle({
68+
onGetSnapshotBeforeUpdate: mockOnGetSnapshotBeforeUpdate,
69+
onDidUpdate: mockOnDidUpdate
70+
})(Target)
71+
const wrapper = mount(
72+
<EnhancedTarget a={1} b={2} />
73+
)
74+
75+
wrapper.setProps({ c: 3 })
76+
77+
expect(mockOnGetSnapshotBeforeUpdate.mock.calls).toMatchSnapshot()
78+
expect(mockOnDidUpdate.mock.calls).toMatchSnapshot()
79+
})
80+
6481
it('onDidUpdate', () => {
6582
const mockOnDidUpdate = jest.fn()
6683
const EnhancedTarget = withLifecycle({ onDidUpdate: mockOnDidUpdate })(Target)
@@ -186,6 +203,25 @@ describe('withLifecycle', () => {
186203
expect(mockOnReceiveProps.mock.calls).toMatchSnapshot()
187204
})
188205

206+
it('onGetSnapshotBeforeUpdate', () => {
207+
const mockOnGetSnapshotBeforeUpdate = jest.fn(() => 'snapshot')
208+
const mockOnDidUpdate = jest.fn()
209+
const EnhancedTarget = withLifecycle(
210+
() => ({
211+
onGetSnapshotBeforeUpdate: mockOnGetSnapshotBeforeUpdate,
212+
onDidUpdate: mockOnDidUpdate
213+
})
214+
)(Target)
215+
const wrapper = mount(
216+
<EnhancedTarget a={1} b={2} />
217+
)
218+
219+
wrapper.setProps({ c: 3 })
220+
221+
expect(mockOnGetSnapshotBeforeUpdate.mock.calls).toMatchSnapshot()
222+
expect(mockOnDidUpdate.mock.calls).toMatchSnapshot()
223+
})
224+
189225
it('onDidUpdate', () => {
190226
const mockOnDidUpdate = jest.fn()
191227
const EnhancedTarget = withLifecycle(

0 commit comments

Comments
 (0)