question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How does react know that a stateless function is a react component

See original GitHub issue

First 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Bogdan-Lyashenkocommented, Jul 28, 2017

@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:

  1. 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.

  2. React.createElement will create an object like

{
	$$typeof: Symbol(react.element),
	key: null,
	props: {props...},
	ref: null, 
	type: ƒ statelessComponent(props),
	...
}
  1. 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.

0reactions
DianaSuvorovacommented, Jul 28, 2017

Thank you!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found