Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

ReactJS State vs props

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

We know that react components are the building blocks that 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 that contains ‘Hello World’ inside it as text. After the function we have a simple javascript function inside it which contains a simple console.log that does the same thing inside it, printing ‘Hello World!’. 

So now we know that a React component works similarly to a normal javascript function. Let’s take a look at the state 

State:

A state is a variable that 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. The state can be asynchronous. Whenever this.setState is used to change the state class is rerender itself.Let’s see with the help of 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 pass the props as attributes and then access them inside our component using this.props. So props make 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
Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials