Skip to content

Commit

Permalink
Warn if childContextType is defined on SFC (#6933)
Browse files Browse the repository at this point in the history
Add console.error message content check

Use appropriate name in warning/test
(cherry picked from commit c47830d)
  • Loading branch information
aweary authored and zpao committed Jun 14, 2016
1 parent e1c2c42 commit 89746fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function warnIfInvalidElement(Component, element) {
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
);
warning(
!Component.childContextTypes,
'%s(...): childContextTypes cannot be defined on a functional component.',
Component.displayName || Component.name || 'Component'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ describe('ReactStatelessComponent', function() {
expect(el.textContent).toBe('mest');
});

it('should warn for childContextTypes on a functional component', () => {
spyOn(console, 'error');
function StatelessComponentWithChildContext(props) {
return <div>{props.name}</div>;
}

StatelessComponentWithChildContext.childContextTypes = {
foo: React.PropTypes.string,
};

var container = document.createElement('div');

ReactDOM.render(<StatelessComponentWithChildContext name="A" />, container);

expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
'be defined on a functional component.'
);
});

it('should warn when stateless component returns array', function() {
spyOn(console, 'error');
function NotAComponent() {
Expand Down

0 comments on commit 89746fa

Please sign in to comment.