Skip to content

Commit

Permalink
Warn if a class is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Sep 27, 2018
1 parent 9009a7b commit a145500
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactPure-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,17 @@ describe('pure', () => {
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 1', 1]);
expect(ReactNoop.getChildren()).toEqual([span(1)]);
});

it('warns for class components', () => {
class SomeClass extends React.Component {
render() {
return null;
}
}
expect(() => pure(SomeClass)).toWarnDev(
'pure: The first argument must be a functional component.',
{withoutStack: true},
);
});
}
});
12 changes: 11 additions & 1 deletion packages/react/src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ export default function pure<Props>(
if (typeof render !== 'function') {
warningWithoutStack(
false,
'pure requires a render function but was given %s.',
'pure: The first argument must be a functional component. Instead ' +
'received: %s',
render === null ? 'null' : typeof render,
);
} else {
const prototype = render.prototype;
if (!!(prototype && prototype.isReactComponent)) {
warningWithoutStack(
false,
'pure: The first argument must be a functional component. Classes ' +
'are not supported. Use React.PureComponent instead.',
);
}
}
}
return {
Expand Down

0 comments on commit a145500

Please sign in to comment.