mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warn if childContextType is defined on SFC (#6933)
Add console.error message content check
Use appropriate name in warning/test
(cherry picked from commit c47830d12c)
This commit is contained in:
committed by
Paul O’Shannessy
parent
e1c2c42c24
commit
89746fa70f
@@ -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'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user