Comments (5)
@DianaSuvorova , I don't think it's a babel. Support of stateless functional components was added in some of the old versions, but it hadn't been like that from the very beginning.
What do you actually mean by 'how does React know'? It doesn't :) I mean, it's not a React component till it's not pasted into other component's render method or ReactDOM.render. Then a ReactElement configuration object will be created for it and after a ReactCompositeComponent object will be instantiated from that configuration object.
Here is how it works:
-
During parsing JSX from component's render method, JSX tags will be replaced by React.createElement calls, but in fact, you can put React.createElement(statelessComponent, ...) directly and it will work (https://facebook.github.io/react/docs/react-without-jsx.html) fine.
-
React.createElement will create an object like
{
$$typeof: Symbol(react.element),
key: null,
props: {props...},
ref: null,
type: ƒ statelessComponent(props),
...
}
- Then, during mounting of its parent the ReactCompositeComponent isntance will be created (by function instantiateReactComponent). 'Function' type of ReactElement is treated in the same way as a class, so ReactCompositeComponent is a mirror component for it.
So, the thing is React allows you to pass into React.createElement
function as type (besides of class, etc.) and knows how to handle it further.
from under-the-hood-reactjs.
Actually, as I typed it I realized that this wouldn't be react it would be babel that would do the work.
More specifically babel react preset.
I will look into it to see how they do it.
from under-the-hood-reactjs.
What do you actually mean by 'how does React know'? It doesn't :) I mean, it's not a React component till it's not pasted into other component's render method or ReactDOM.render.
That explains it! So react only knows it during runtime. I was looking for a static analysis method. But this makes sense!
Can you please point me to the source code for the React.createElement
(or your documentation of it)? Where it creates the object you described.
Thank you!
from under-the-hood-reactjs.
Hey, please check the following https://github.com/facebook/react/blob/357925a84e34d17b96568e67d30baeb2a399da11/src/isomorphic/classic/element/ReactElement.js#L182
from under-the-hood-reactjs.
Thank you!
from under-the-hood-reactjs.
Related Issues (18)
- Can I translate this repository into Chinese? HOT 8
- Picture Intro.3 has overlapping elements where the name is not visible HOT 1
- Korean translate HOT 5
- How was this achieved? HOT 3
- Help With Fiber HOT 5
- Setting up the infrastructure. HOT 3
- We have translation this repo into Chinese, do you consider add a ch-zn branch for Chinese version? HOT 3
- Use "React" instead of "ReactJS" HOT 1
- Possible typos in SVGs - part 7
- Possible text formatting issue in part 9 HOT 3
- Broken link to previous page in Part 13
- Updates HOT 1
- Russian translation HOT 1
- Under the hood react js
- Favicon Missing for Under-the-hood-ReactJS HOT 1
- What does the "clickable" mean in the title? HOT 2
- which tool you use to draw picture? HOT 1
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 under-the-hood-reactjs.