DEV Community

Cover image for State vs Props in reactjs
Sakib Ahmed
Sakib Ahmed

Posted on

State vs Props in reactjs

What is State?

The state is an update-able structure that is used to contain data or information about the component and can change over time. The change in state can happen as a response to user action or system event. It is the heart of the react component which determines the behavior of the component and how it will render. A state must be kept as simple as possible. It represents the component's local state or information. it can only be accessed or modified inside the component or by the component directly.

What is Props?

Props are read-only component. It is an object which stores the value of attributes of a tag and work similar to the HTML attributes. It allows passing data from one component to other components. It is similar to function arguments and can be passed to the component the same way as arguments passed in a function. Props are immutable so we cannot modify the props from inside the component.

Dissimilarity between State and Props

1.
State: State changes can be asynchronous.
Props: Props are read-only.

2.
State: State is mutable.
Props: Props are immutable.

3.
State: State holds information about the components.
Props: Props allow you to pass data from one components to other components as an argument.

4.
State: State can be used for rendering dynamic changes with the component.
Props: Props used to communicate between components.

5.
State: State cannot be accessed by child components.
Props: Props can be accessed by child components.

6.
State: The state is internal and controlled by the React Component itself.
Props: Props are external and controlled by what ever renders the component.

7.
State: State cannot make components reusable.
Props: Props make components reusable.

Below table will guide you about the changing in props and state.

NO Condition State Props
1. Can get initial value from parent component? Yes Yes
2. Can be changed by parent Component? No Yes
3. Can set default values inside component? Yes Yes
4. Can change inside component? Yes No
5. Can set initial value for child component? Yes Yes
6. Can change in child component? Yes No

And last but not least, The component state and props share some common similarities.

NO State and Props
1. Both are plain JavaScript Object.
2. Both can contain default values.
3. Both are read-only when they are using this.

See yaaa.

devvsakib

Top comments (0)