2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: State vs Props
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## State vs Props
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-13 13:38:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								When we start working with React components, we frequently hear two terms. These are `state`  and `props` . So, in this article we will explore what those are and how they differ.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## State:
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  State is something that a component owns. It belongs to that particular component where it is defined. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, A person's age is a state of that person.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-13 13:38:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  State is mutable. But it can be changed by only by the component itself, meaning it is private. As I only can change my age, no one else can. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  You can change the state by using `this.setState()`  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								See the below example to get an idea of state:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Person.js
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  import React from 'react';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  class Person extends React.Component{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.state = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        age:0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.incrementAge = this.incrementAge.bind(this)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    incrementAge(){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        age:this.state.age + 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    render(){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          < label > My age is: {this.state.age}< / label > 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-13 13:38:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          < button  onClick = {this.incrementAge} > Grow me older !!< / button > 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  export default Person;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In the above example,  `age`  is the state of `Person`  component. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Props:
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-13 13:38:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  Props are similar to method arguments. They are passed to a component when it is instantiated. 
						 
					
						
							
								
									
										
										
										
											2018-10-30 11:57:18 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  Props are immutable. They are read-only. 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								See the below example to get an idea of Props:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Person.js
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  import React from 'react';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  class Person extends React.Component{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    render(){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          < label > I am a {this.props.character} person.< / label > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  export default Person;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const person = < Person  character  =  "good" > < / Person > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In the above example, `const person = <Person character = "good"></Person>`  we are passing `character = "good"`  prop to `Person`  component.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It gives output as "I am a good person", in fact I am.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								There is lot more to learn on State and Props. Many things can be learnt by actually diving into coding. So get your hands dirty by coding.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Happy Coding !!!