I tried the React documentation, but couldn't find anything about reserved words. I was wondering: is state
in React a reserved word?
Is "state" a reserved word in React?
Asked Answered
No keyword comes to mind that are reserved in React specifically. JSX is a superset of JavaScript which mostly adds HTML-like syntax to it. –
Chibchan
I was wondering how setState works, which led me to this question, for what it's worth. @Kaca992 below answers this particular question. –
Eadwine
No state is not a reserved word. I don’t think there are any specific to react but there are reserved keywords in javascript language.
At this Pen, when I replace all the "state" words, things stop working. I'm confused and clearly missing something. –
Flybynight
@RogerB.H.
this.state
is a property of a React.Component
class, which you can't change and still expect it to work. The React code behind uses it, if you change it, React doesn't know about your new key and it doesn't provide a way to customize it unfortunately. –
Chibchan Yes in class components
state
and props
are properties of the context, i.e. this
. –
Marinara @MuhammadAli, So, in class components
state
and props
are reserved words? –
Flybynight @RogerB.H. it's not that it's reserved, it's just that it is how the API is defined. –
Chibchan
not exactly but you can’t directly rename state and props from context of the component. –
Marinara
Let me see... Can I put it this way? In the context of a React.Component, yes, the
state
is a "reserved" word and one must not change it. But, anywhere else in one's React project, the word state
can be used, even if it makes the code a bit confusing? –
Flybynight @RogerB.H. Redux also uses the word
state
, but it's not the same React state. state
is a generic word, use it whenever it makes sense to you and just be careful not to overwrite/shadow an existing property or variable. ;) –
Chibchan I marked this answer as correct because of all the collaborations in the comments. –
Flybynight
State is simply a property inside React.Component. Nothing special about it.
Well, there is something special about it. It's not a keyword as such, but within a react component, it effectively is like a keyword, in that it's already defined. setState will be looking for a thing called "state" to do its state-setting. I'm kind of a beginner at React, so I'll gladly take correction if this comment is inaccurate. –
Eadwine
No state is not a reserved word. I don’t think there are any specific to react but there are reserved keywords in javascript language.
At this Pen, when I replace all the "state" words, things stop working. I'm confused and clearly missing something. –
Flybynight
@RogerB.H.
this.state
is a property of a React.Component
class, which you can't change and still expect it to work. The React code behind uses it, if you change it, React doesn't know about your new key and it doesn't provide a way to customize it unfortunately. –
Chibchan Yes in class components
state
and props
are properties of the context, i.e. this
. –
Marinara @MuhammadAli, So, in class components
state
and props
are reserved words? –
Flybynight @RogerB.H. it's not that it's reserved, it's just that it is how the API is defined. –
Chibchan
not exactly but you can’t directly rename state and props from context of the component. –
Marinara
Let me see... Can I put it this way? In the context of a React.Component, yes, the
state
is a "reserved" word and one must not change it. But, anywhere else in one's React project, the word state
can be used, even if it makes the code a bit confusing? –
Flybynight @RogerB.H. Redux also uses the word
state
, but it's not the same React state. state
is a generic word, use it whenever it makes sense to you and just be careful not to overwrite/shadow an existing property or variable. ;) –
Chibchan I marked this answer as correct because of all the collaborations in the comments. –
Flybynight
© 2022 - 2024 — McMap. All rights reserved.