Comments (5)
https://overreacted.io/how-does-react-tell-a-class-from-a-function/ allows checking this. I think this is somewhat considered a public API. I couldn't find any rationale why React users should be able to do this check. The quote is written from the perspective of React internals.
The last thing I'd wish to add is about known buggy behaviour of Babel.js: there is no difference between class, function or arrow function after transpilation.
class
is just syntactic sugar. Even without transpilation there's no 100% accurate way to determine if something is a class or a function.
With regards to function vs arrow function: Why should this matter? You can't distinguish that with you babel setup because you probably transpile for a target that doesn't support arrow functions.
from rfcs.
If you have comments for that RFC, please leave it on the RFC.
from rfcs.
0.
Thank you for the answer @eps1lon!
The link to how-does-react-tell-a-class-from-a-function is wonderful!
And that article is an answer to the issue! Thank you again!
There was no arguing for or against something. We were just seeking whether this useful or not. And if it might be helpful somehow, maybe, probably, perhaps. And article clearly:
- points to
.prototype
property descriptor for class:writeable
=== false; - refers to the known stuff about babel transpilation: they are not going to give any solution;
- explains why
isReactComponent
matters: the article itself.
In relation to the other questions mentioned :
1.
rationale why React users should be able to do this check
We were just wondering if we could share some useful information for the core. And now it seems useless as its something well-known. Just because it was written here 0000-create-element-changes#deprecate-module-pattern-components
The important goal here is that if we're going to introduce different semantics between classes and function components, we need to know before calling them which semantics we're going to apply.
, and we thought it is some sort of thing that is hard to deal with.
So, finally, there is nothing to help with, it is just the way it is )
2.
class is just syntactic sugar. Even without transpilation there's no 100% accurate way to determine if something is a class or a function.
It is true that class works like sugar. And it is a bit tricky to implement class without using class
keyword, especially the extends
part:
- We might be dealing with functions as classes. We might prohibit their behaviour without
new
keyword, usingnew.target
. Butnew.target
itself is a comparatively new feature. - Also, the
.getPrototypeOf
of extended class refers to the base class. And on the other hand the.prototype
property itself is not re-writeable for classes. Though again, it is a new syntax from ES2015 improvements. And it might be overcomplicated journey withObject.setPrototypeOf
,Object.defineProperty
and'use strict';
even having ES2015 as a starting point. - And before are going to deal with the above mentions, is necessity to deal with
MyClass.prototype.constructor
properly. And even after that there are some misconfigurations and other details with private fields and mixins. And so far is it worth getting so deep inside?
Perhaps that is why class
seems to be like syntactic sugar, as transpilation does not follow each and every part of the spec. And even if it seems they are syntactic sugar, there are other parts of class constructor for example. So class keyword is not useless. But in a relation of how infrastructure of transpilation works they are not so useful, yes.
3.
With regards to function vs arrow function: Why should this matter? You can't distinguish that with you babel setup because you probably transpile for a target that doesn't support arrow functions.
Yes, yes, the same, it is clearly explained with the information in the article provided.
It is this way because it is aways transpiled to a function )
from rfcs.
There is a way to fix this behaviour:
for Arrow Functions
const workingArrow = () => {};
// right after arrow function definition
Object.defineProperty(workingArrow, 'prototype', {
value: undefined
});
for Classes
class WorkingClass {}
// right after class definition
Object.defineProperty(WorkingClass, 'prototype', {
writable: false
});
Just in case it might worth opening PR to babel, what do you think?
from rfcs.
Tried to start PR for babel babel/babel#12115
from rfcs.
Related Issues (20)
- onScroll event propagation HOT 5
- [Feature Request]: distinguish "what" and "when" dependencies in useEffect HOT 2
- [Feature Request] Return ref rather than forwardRef HOT 1
- npx-create-react-app creating a folder tempnodejsnpm HOT 2
- [Feature Request] Can we do some Static Analysis for diff with babel ? HOT 6
- Improving the RFC workflow process HOT 18
- useIf: Conditional hooks HOT 6
- Is useReducer an overengineering? HOT 10
- Improve profiling react applications HOT 1
- Introduce GUI tooling to speed up web application development HOT 1
- [React Server Components] Idea to simplify overall design HOT 11
- psql: could not connect to server: No such file or directory HOT 1
- [Question] The new JSX transform HOT 1
- [Feature Request]: Add array of updated deps indices to `useEffect` hooks arg HOT 1
- [Feature Request]: React Hooks `Equality` **AKA:** `[isEqual]` Callback HOT 3
- Functional Attribute/Prop Node HOT 2
- [Feature Request][eslint-plugin-react-hooks] no-ref-checks, display error when using useRef's return value as condition HOT 1
- [Feature Request]: useStateRef HOT 5
- Typo: 'exiting' might be 'existing' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rfcs.