Warn If a Component Doesn't Extend React.Component #4599
Comments
What about checking if |
@rads 👍🏻 |
@rads That might be enough. A bit fragile. It also means that classes in ECMAScript can never have a default We might have more than one way of rendering in the future. E.g. Will need to check if there's any difference in terms of engine optimizations between these options. |
I don't think duck-typing is the way to go, much too fragile. having to extend |
Another solution would be to only allow arrow functions as plain functions since they have undefined prototypes they're easily detectable. E.g. |
@sebmarkbage Is there a performant way for transpilers to change this behavior? |
But if component is stateless you can just turn it into a function? |
Asking users to brand classes with a Symbol would probably be too awkward? |
With decorators seems more better here. But of course it's too early for decorators. |
excited to see this happen. will this also mean not needing to wrap JSX 'interpolated' strings with s? eg |
@threepointone No, that's still the case. This is unrelated. |
@sebmarkbage Want to do the prototype check or the static flag? |
Extending |
@sebmck Any plans on giving arrow functions and concise methods Basically, we're working around the fact that we can't distinguish them from a plain function which is ambivalent with regard if it should be constructable or not. I would go as far as saying that function expression/declarations should be considered deprecated as of ES6, and best practice should be to use any of the other forms of creation a function. |
Not really. Although recently I've been leaning more towards correctness over performance, eg. Babel 6.0.0 is going to have TDZ by default. There'd be a bunch of cases we could statically analyse and omit the
Probably wouldn't go that far, I can already feel the pitchforks being raised |
To support arrow functions and plain functions as "components" we need to know if we can call new on them. Examples that won't work:
We need to detect if a component is a "function" or a "class" before calling
new
on it.We can call
new
on everything if they're plain functions as long as they return aReactElement
. However, that won't work for null/false/string return values and we want to support those too. We also can't callnew
on arrow functions. Likewise, we can't NOT callnew
on classes.Unfortunately ECMAScript doesn't have a way to detect if something is
new
able or not.The current plan is to add our own static flag to React.Component so that we can detect if something is a class.
Any class pattern that doesn't require
new
should still be possible:I'm not sure how we can distinguish this pattern from one that requires
new
for warnings though. :(The text was updated successfully, but these errors were encountered: