Skip to content

Commit 5d22fe4

Browse files
committed
Warn if first argument is not a functional component
1 parent 3cb0a3f commit 5d22fe4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/react-reconciler/src/__tests__/ReactPure-test.internal.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,25 @@ describe('pure', () => {
170170
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 1', 1]);
171171
expect(ReactNoop.getChildren()).toEqual([span(1)]);
172172
});
173+
174+
it('warns for class components', () => {
175+
class SomeClass extends React.Component {
176+
render() {
177+
return null;
178+
}
179+
}
180+
expect(() => pure(SomeClass)).toWarnDev(
181+
'pure: The first argument must be a functional component.',
182+
{withoutStack: true},
183+
);
184+
});
185+
186+
it('warns if first argument is not a function', () => {
187+
expect(() => pure()).toWarnDev(
188+
'pure: The first argument must be a functional component. Instead ' +
189+
'received: undefined',
190+
{withoutStack: true},
191+
);
192+
});
173193
}
174194
});

packages/react/src/pure.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ export default function pure<Props>(
1717
if (typeof render !== 'function') {
1818
warningWithoutStack(
1919
false,
20-
'pure requires a render function but was given %s.',
20+
'pure: The first argument must be a functional component. Instead ' +
21+
'received: %s',
2122
render === null ? 'null' : typeof render,
2223
);
24+
} else {
25+
const prototype = render.prototype;
26+
if (!!(prototype && prototype.isReactComponent)) {
27+
warningWithoutStack(
28+
false,
29+
'pure: The first argument must be a functional component. Classes ' +
30+
'are not supported. Use React.PureComponent instead.',
31+
);
32+
}
2333
}
2434
}
2535
return {

0 commit comments

Comments
 (0)