Warn if childContextType is defined on SFC (#6933)
Add console.error message content check Use appropriate name in warning/test
Brandon Dail committed
Jun 8, 2016 at 22:32 UTC
c47830d12c495bd8e02c5e84f2de9e9384165047
2 files changed
+26
src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
+5
@@ -46,6 +46,11 @@ function warnIfInvalidElement(Component, element) {
46
'returned undefined, an array or some other invalid object.',
47
Component.displayName || Component.name || 'Component'
48
);
49
+ warning(
50
+ !Component.childContextTypes,
51
+ '%s(...): childContextTypes cannot be defined on a functional component.',
52
+ Component.displayName || Component.name || 'Component'
53
+ );
54
}
55
}
56
src/renderers/shared/stack/reconciler/__tests__/ReactStatelessComponent-test.js
+21
@@ -98,6 +98,27 @@ describe('ReactStatelessComponent', function() {
98
expect(el.textContent).toBe('mest');
99
});
100
101
+ it('should warn for childContextTypes on a functional component', () => {
102
+ spyOn(console, 'error');
103
+ function StatelessComponentWithChildContext(props) {
104
+ return <div>{props.name}</div>;
105
+ }
106
+
107
+ StatelessComponentWithChildContext.childContextTypes = {
108
+ foo: React.PropTypes.string,
109
+ };
110
+
111
+ var container = document.createElement('div');
112
+
113
+ ReactDOM.render(<StatelessComponentWithChildContext name="A" />, container);
114
+
115
+ expect(console.error.calls.count()).toBe(1);
116
+ expect(console.error.calls.argsFor(0)[0]).toContain(
117
+ 'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
118
+ 'be defined on a functional component.'
119
+ );
120
+ });
121
+
122
it('should warn when stateless component returns array', function() {
123
spyOn(console, 'error');
124
function NotAComponent() {