Skip to content

Commit 20de413

Browse files
committed
Remove usages of async-unsafe lifecycle methods
1 parent 6ad378b commit 20de413

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ It does not modify the component class passed to it; instead, it *returns* a new
377377

378378
* [`renderCountProp`] *(String)*: if defined, a property named this value will be added to the props passed to the wrapped component. Its value will be the number of times the component has been rendered, which can be useful for tracking down unnecessary re-renders. Default value: `undefined`
379379

380-
* [`shouldHandleStateChanges`] *(Boolean)*: controls whether the connector component subscribes to redux store state changes. If set to false, it will only re-render on `componentWillReceiveProps`. Default value: `true`
380+
* [`shouldHandleStateChanges`] *(Boolean)*: controls whether the connector component subscribes to redux store state changes. If set to false, it will only re-render when parent component re-renders. Default value: `true`
381381

382382
* [`storeKey`] *(String)*: the key of props/context to get the store. You probably only need this if you are in the inadvisable position of having multiple stores. Default value: `'store'`
383383

src/components/Provider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export function createProvider(storeKey = 'store', subKey) {
3838
}
3939

4040
if (process.env.NODE_ENV !== 'production') {
41-
Provider.prototype.componentWillReceiveProps = function (nextProps) {
42-
if (this[storeKey] !== nextProps.store) {
41+
Provider.prototype.componentDidUpdate = function () {
42+
if (this[storeKey] !== this.props.store) {
4343
warnAboutReceivingStore()
4444
}
4545
}

src/components/connectAdvanced.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,10 @@ export default function connectAdvanced(
156156
if (this.selector.shouldComponentUpdate) this.forceUpdate()
157157
}
158158

159-
componentWillReceiveProps(nextProps) {
160-
this.selector.run(nextProps)
161-
}
162-
163-
shouldComponentUpdate() {
159+
shouldComponentUpdate(nextProps) {
160+
if (nextProps !== this.props) {
161+
this.selector.run(nextProps)
162+
}
164163
return this.selector.shouldComponentUpdate
165164
}
166165

@@ -248,6 +247,10 @@ export default function connectAdvanced(
248247

249248
render() {
250249
const selector = this.selector
250+
251+
// Handle forceUpdate
252+
if (!selector.shouldComponentUpdate) selector.run(this.props)
253+
251254
selector.shouldComponentUpdate = false
252255

253256
if (selector.error) {
@@ -265,7 +268,7 @@ export default function connectAdvanced(
265268
Connect.propTypes = contextTypes
266269

267270
if (process.env.NODE_ENV !== 'production') {
268-
Connect.prototype.componentWillUpdate = function componentWillUpdate() {
271+
Connect.prototype.componentDidUpdate = function componentDidUpdate() {
269272
// We are hot reloading!
270273
if (this.version !== version) {
271274
this.version = version
@@ -287,6 +290,8 @@ export default function connectAdvanced(
287290
this.subscription.trySubscribe()
288291
oldListeners.forEach(listener => this.subscription.listeners.subscribe(listener))
289292
}
293+
294+
if (this.selector.shouldComponentUpdate) this.setState(dummyState)
290295
}
291296
}
292297
}

test/components/Provider.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,19 @@ describe('React', () => {
220220
store.dispatch({ type: 'APPEND', body: 'd' })
221221
expect(childMapStateInvokes).toBe(4)
222222
})
223+
224+
it('works in <StrictMode> without warnings', () => {
225+
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
226+
const store = createStore(() => ({}))
227+
228+
TestRenderer.create(
229+
<React.StrictMode>
230+
<Provider store={store}>
231+
<div />
232+
</Provider>
233+
</React.StrictMode>
234+
)
235+
236+
expect(spy).not.toHaveBeenCalled()
237+
})
223238
})

test/components/connect.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('React', () => {
200200

201201
@connect(state => ({ string: state }) )
202202
class Container extends Component {
203-
componentWillMount() {
203+
componentDidMount() {
204204
store.dispatch({ type: 'APPEND', body: 'a' })
205205
}
206206

@@ -944,8 +944,8 @@ describe('React', () => {
944944

945945
@connect((state) => ({ state }))
946946
class Child extends Component {
947-
componentWillReceiveProps(nextProps) {
948-
if (nextProps.state === 'A') {
947+
componentDidMount() {
948+
if (this.props.state === 'A') {
949949
store.dispatch({ type: 'APPEND', body: 'B' });
950950
}
951951
}
@@ -1927,7 +1927,7 @@ describe('React', () => {
19271927

19281928
@connect(mapStateFactory)
19291929
class Container extends Component {
1930-
componentWillUpdate() {
1930+
componentDidUpdate() {
19311931
updatedCount++
19321932
}
19331933
render() {
@@ -2008,7 +2008,7 @@ describe('React', () => {
20082008

20092009
@connect(null, mapDispatchFactory, mergeParentDispatch)
20102010
class Passthrough extends Component {
2011-
componentWillUpdate() {
2011+
componentDIdUpdate() {
20122012
updatedCount++
20132013
}
20142014
render() {
@@ -2120,7 +2120,7 @@ describe('React', () => {
21202120

21212121
@connect(null)
21222122
class Parent extends React.Component {
2123-
componentWillMount() {
2123+
UNSAFE_componentWillMount() {
21242124
this.props.dispatch({ type: 'fetch' })
21252125
}
21262126

0 commit comments

Comments
 (0)