How does react know that a stateless function is a react component
See original GitHub issueFirst of all thanks for publishing this. This looks like an enormous effort that would benefit a lot of people.
I am working on a few issues for https://github.com/yannickcr/eslint-plugin-react library and have very specific question that you might have an answer for.
How does react know that a stateless function is a react component. For example:
const statelessComponent = (props) => {
return <span>{props.someProp}</span>;
}
If you could point to a source code where react evaluate whether a function is a React or not that would be awesome. Any other directions would also be greatly appreciated
Thanks, Diana
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Learn Stateful and Stateless components in ReactJS - CronJ
A stateless component renders output which depends upon props value, but a stateful component render depends upon the value of the state. A...
Read more >Stateful and Stateless Components in React
Today, we're going to review what stateful and stateless components are in React, how you can tell the difference, and the complex process ......
Read more >Stateless Components From Stateful Components Cheatsheet
In React, a stateful component is a component that holds some state. Stateless components, by contrast, have no state. Note that both types...
Read more >React: Class Component vs Functional Component - ITNEXT
A functional(a.k.a. stateless) component is just a plain javascript function which takes props as an argument and returns a react element. const ...
Read more >Stateful vs. Stateless Functional Components in React - Code
Stateful components are either class components or functional components with hooks. Most stateful components use hooks nowadays, but class ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
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.Thank you!