laxman
laxman

Reputation: 2110

what does this.state references to in a react component?

I have just started working in react since couple of days and I am confused with following

  1. If each react component have state isolated to the scope of that component or is there only one state for complete app ?

  2. How does react differentiates between a Component function and a normal function? Is it the return value?

    const component = () => ( //jsx DOM element );

    const obj = () => 'return value';

Thanks.

Upvotes: 0

Views: 67

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 85012

If each react component have state isolated to the scope of that component or is there only one state for complete app ?

It's an instance property of the individual component, which is why you access it via this.state. It is not global to the app.

How does react differentiates between a Component function and a normal function? Is it the return value?

It doesn't. If you take a function that was never meant to be used as a component, and then use it as a component, react will do as you ask, but the results will probably not be useful.

For example, if you create a function that looks like this:

const Sum = (a, b) => a + b;

And then try to render the following:

<div>
  <Sum />
</div>

React will not throw any errors or anything, but you'll see [object Object][object Object] on the page

Upvotes: 3

Related Questions