Props |
State |
Immutable |
Owned by its component |
Has better performance |
Locally scoped |
Can be passed to child components |
Witeable/Mutable |
|
has setState() method to modify properties |
|
Changes to state can be asynchronous |
|
can only be passed as props |
React State
Every component in react has a built-in state object, which contains all the property values that belong to that component.
In other words, the state object controls the behaviour of a component. Any change in the property values of the state object leads to re-rendering of the component.
**Note- State object is not available in functional components but, we can use React Hooks to add state to a functional component.
How to declare a state object?
Example:
class Car extends React.Component{
constructor(props){
super(props);
<strong style="-webkit-font-smoothing:subpixel-antialiased; border:0px; box-sizing:border-box; color:#008800; font-family:Helvetica,Arial,Sans-serif; font-size:15px; font-weight:bold; margin:0px; padding:0p