Skip to content

Commit 09c39d0

Browse files
committed
Warn about UNSAFE_componentWillRecieveProps misspelling
1 parent b71ca93 commit 09c39d0

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ export default function(
330330
'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?',
331331
name,
332332
);
333+
const noUnsafeComponentWillRecieveProps =
334+
typeof instance.UNSAFE_componentWillRecieveProps !== 'function';
335+
warning(
336+
noUnsafeComponentWillRecieveProps,
337+
'%s has a method called ' +
338+
'UNSAFE_componentWillRecieveProps(). Did you mean UNSAFE_componentWillReceiveProps()?',
339+
name,
340+
);
333341
const hasMutatedProps = instance.props !== workInProgress.pendingProps;
334342
warning(
335343
instance.props === undefined || !hasMutatedProps,

packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,23 @@ describe 'ReactCoffeeScriptClass', ->
410410
)
411411
undefined
412412

413+
it 'should warn when misspelling UNSAFE_componentWillReceiveProps', ->
414+
class NamedComponent extends React.Component
415+
UNSAFE_componentWillRecieveProps: ->
416+
false
417+
418+
render: ->
419+
span
420+
className: 'foo'
421+
422+
expect(->
423+
test React.createElement(NamedComponent), 'SPAN', 'foo'
424+
).toWarnDev(
425+
'Warning: NamedComponent has a method called UNSAFE_componentWillRecieveProps().
426+
Did you mean UNSAFE_componentWillReceiveProps()?'
427+
)
428+
undefined
429+
413430
it 'should throw AND warn when trying to access classic APIs', ->
414431
instance =
415432
test Inner(name: 'foo'), 'DIV', 'foo'

packages/react/src/__tests__/ReactES6Class-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,23 @@ describe('ReactES6Class', () => {
433433
);
434434
});
435435

436+
it('should warn when misspelling UNSAFE_componentWillReceiveProps', () => {
437+
class NamedComponent extends React.Component {
438+
UNSAFE_componentWillRecieveProps() {
439+
return false;
440+
}
441+
render() {
442+
return <span className="foo" />;
443+
}
444+
}
445+
446+
expect(() => test(<NamedComponent />, 'SPAN', 'foo')).toWarnDev(
447+
'Warning: ' +
448+
'NamedComponent has a method called UNSAFE_componentWillRecieveProps(). ' +
449+
'Did you mean UNSAFE_componentWillReceiveProps()?',
450+
);
451+
});
452+
436453
it('should throw AND warn when trying to access classic APIs', () => {
437454
const instance = test(<Inner name="foo" />, 'DIV', 'foo');
438455
expect(() =>

packages/react/src/__tests__/ReactTypeScriptClass-test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ class MisspelledComponent2 extends React.Component {
273273
}
274274
}
275275

276+
// it should warn when misspelling UNSAFE_componentWillReceiveProps
277+
class MisspelledComponent3 extends React.Component {
278+
UNSAFE_componentWillRecieveProps() {
279+
return false;
280+
}
281+
render() {
282+
return React.createElement('span', {className: 'foo'});
283+
}
284+
}
285+
276286
// it supports this.context passed via getChildContext
277287
class ReadContext extends React.Component {
278288
static contextTypes = {bar: PropTypes.string};
@@ -537,6 +547,16 @@ describe('ReactTypeScriptClass', function() {
537547
);
538548
});
539549

550+
it('should warn when misspelling UNSAFE_componentWillReceiveProps', function() {
551+
expect(() =>
552+
test(React.createElement(MisspelledComponent3), 'SPAN', 'foo')
553+
).toWarnDev(
554+
'Warning: ' +
555+
'MisspelledComponent3 has a method called UNSAFE_componentWillRecieveProps(). ' +
556+
'Did you mean UNSAFE_componentWillReceiveProps()?'
557+
);
558+
});
559+
540560
it('should throw AND warn when trying to access classic APIs', function() {
541561
const instance = test(
542562
React.createElement(Inner, {name: 'foo'}),

0 commit comments

Comments
 (0)