State & Props

Before jumping to state vs props we have to compare a React component with a javascript plain function. Let me define a React component & a plain javascript function side by side.

class DummyComponent extends React.Component {
    render () {
        return <div>Hey</div>
    }
}
const DummyFunction = () => console.log('Hey')

We defined a React component named DummyComponent and returned a divcontaining text Hey similarly, we defined a function named DummyFunctionand output Hey to the console. Isn’t they look a lot similar ? they both generate the same output write Hey to the output the only difference between the two is the React component going to render a div with text Heywhere the function going to output Hey on the console.

So we now Know that React components are very similar to Plain Javascript functions. Let’s take a look at State.

React Component State

A state in React Component is its own local state, the state cannot be accessed and modified outside the component and can only be used inside the component which is very similar to, you already guessed it a function own local scope. We can define variables inside the function which can only be used inside the function block scope. Let’s demonstrate this with an example.


class DummyComponent extends React.Component {
  state = {
    name: 'Manoj'
  }
  render() {
    return <div>Hello {this.state.name}</div>;
  }
  
}
const DummyFunction = () => {
  let name = 'Manoj';
  console.log(`Hey ${name}`)
}

As you can see a component state can be compared to a function local scope. We defined a name property inside Component state and used it inside the render process. Similarly inside the function we defined a variable name and used it inside the function. Everything good on the state ? Now let’s talk about props next.

Component Props

Without props, React Component will stop making sense. A React component is a reusable component which can be used over and over again in the UI, but not always we are going to render the same component with same data. Sometimes we have to change the data or content inside a component. That’s why props are introduced in React. Let’s take a look how we can use props in react.

class DummyComponent extends React.Component {
  render() {
    return <div>Hello {this.props.name}</div>;
  }
  
}
// when using the component
<DummyComponent name="Manoj" />
<DummyComponent name="Ajay" />

We used one react component in multiple places here but with a different name. As you can see we can pass props to our React component using attributes and then can access them inside our component using this.propspretty straightforward. Props are similar to passed arguments to a function. Here is an example :-

const DummyFunction = (name) => {
  console.log(`Hey ${name}`)
}
DummyFunction('Manoj');
DummyFunction('Ajay');

We passed argument name to the function and then used it similarly to when we passed props to the component. Props make react component reusable so you can use the same component with different data every time. The same principle is used when creating functions we create a function with parameters so we can pass them different arguments every time and get different results.

State is referred to the local state of the component which cannot be accessed and modified outside of the component and only can be used & modified inside the component. Props, on the other hand make components reusable by giving components the ability to receive data from the parent component in the form of props.

Last updated