Warn if class has a render() method but doesn't extend React.Component #11168
Conversation
|
Prettier is failing. Have you run |
|
gosh really embarrassing. Looks like i didnt commit the prettified file. I have resubmitted and it should pass. Jeez I am going to be so embarrassed when I look back on this in a few months |
|
See changes below + we'll need a test for this. |
| warning( | ||
| !(workInProgress.type.prototype && | ||
| workInProgress.type.prototype.render), | ||
| 'Warning: Using a class with a render method as a function, ' + |
| @@ -481,6 +481,12 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>( | |||
| var value; | |||
|
|
|||
| if (__DEV__) { | |||
| warning( | |||
| !(workInProgress.type.prototype && | |||
gaearon
Oct 10, 2017
Member
This is a bit hard to follow. Let's just wrap it in an if block with the real condition, and then pass false to the warning call.
| !(workInProgress.type.prototype && | ||
| workInProgress.type.prototype.render), | ||
| 'Warning: Using a class with a render method as a function, ' + | ||
| 'did you forget to extend React.Component?', |
gaearon
Oct 10, 2017
Member
I would tweak the wording to:
The <%s /> component appears to have a render method, but doesn't extend React.Component. This is likely to cause errors. Change %s to extend React.Component instead.
You’ll need to pass the component name (use getComponentName).
|
thanks @gaearon . its not clear to me -where- to add the tests. is there a deeper test guide somewhere other than the 4 page Contributing docs?
|
|
added the test in |
|
Sorry the structure is a bit confusing. We haven’t updated the file structure to be sensible yet after deleting the old engine. But I’d put it here. |
|
Also the test is failing:
Have you tried running it with |
|
seems i errantly deleted a space when I made the warning multiline. will fix and move as suggested. Yeah is there a separate issue for restructuring the test file structure? "ReactIncremental" doesnt really seem to make sense anymore |
|
Sorry for the trouble again. We rearranged how files are laid out in the repo. Could you please reapply your changes on top of master? |
merging in fb's changes
|
I confess I'm not sure what's going on. I thought I could simply |
|
ok this one looks like it works. Woohoo! |
| try { | ||
| ReactDOM.render(<ClassWithRenderNotExtended />, container); | ||
| } catch (e) { | ||
| expect(e).toEqual(new TypeError('Cannot call a class as a function')); |
gaearon
Oct 31, 2017
Member
This only runs assertions if we get into catch. But what if we have a bug that causes us not to throw an error? Then the test doesn't assert anything.
|
I made a few minor tweaks:
I think this is good to go. Thanks! |
7f10fae
into
facebook:master
facebook#11168) Warn if class has a render() method but doesn't extend React.Component


Hi! adding the fix for #10103. test/prettier/lint/flow all done and passing. (replaces PR #11149)