Skip to content

Commit

Permalink
Warn if first argument is not a functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Sep 27, 2018
1 parent 3cb0a3f commit 645a8ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 20 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,25 @@ 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},
);
});

it('warns if first argument is not a function', () => {
expect(() => pure()).toWarnDev(
'pure: The first argument must be a functional component. Instead ' +
'received: undefined',
{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 645a8ba

Please sign in to comment.