Skip to content
Related Articles
Open in App
Not now

Related Articles

ReactJS | State vs props

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 30 Jul, 2020
Improve Article
Save Article
Like Article

We know that in react components are the building blocks which can be reused again and again in building the UI. Before jumping into the main difference between the state and props, let’s see how a component in react is related to a normal function in javascript 
Example: 
 

javascript




// simple component
class FakeComponent extends React.component{
 render(){
    return <div>Hello World!</div>
    }
}
// simple javascript function
const FakeFunction = () => console.log('Hello World!');


In the above code we declared a simple react component by extending the React.component native method and then we simply render a div which contains ‘Hello World’ inside it as text. After the function we have a simple javascript function inside it which contains a simple console.log which does the same thing inside it, printing ‘Hello World!’. 
So now we know that a React component works similar to a normal javascript function. Let’s take a look at state 
 

State

A state is a variable which exists inside a component, that cannot be accessed and modified outside the component and can only be used inside the component. Works very similarly to a variable that is declared inside a function that cannot be accessed outside the scope of the function in normal javascript.State Can be modified using this.setState. State can be asynchronous.Whenever this.setState is used to change the state class is rerender itself.Let’s see with the help an example: 
Example: 
 

javascript




// component
class FakeComponent extends React.component{
  state = {
      name : 'Mukul';
   }
  render(){
      return <div>Hello {this.state.name}</div>
   }
}
// simple js function
const FakeFunction = () => {
  let name = 'Mukul';
  console.log(`Hey ${name}`);
}


In the above code we simply declare a name property inside a component and used it while rendering, similar is the case with a normal function in javascript. It should also be noted that the state is mutable in nature, and should not be accessed from child components. 
 

Props

We know that components in React are used again and again in the UI, but we don’t normally render the same component with the same data. Sometimes we need to change the content inside a component. Props come to play in these cases, as they are passed into the component and the user. Let’s see how they work: 
Example: 
 

javascript




// component
class FakeComponent extends React.component{
 render(){
    return <div>Hello {this.props.name}</div>
    }
}
// passing the props
<FakeComponent name='Mukul' />
<FakeComponent name='Mayank' />


A simple component and then we passes the props as attributes and then access them inside our component using this.props. So props makes components reusable by giving components the ability to receive data from the parent component in the form of props. They are immutable.
 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!