React State and Props

React state and props are two fundamental concepts in React.js that are essential to building reusable and dynamic components. Here is a table that summarizes the similarities and differences between React state and props:

Table 1
Column 1
To manage and control the internal state of a component
To pass data from a parent component to a child component
Type of data
Setting values
Using setState() method
Passed down from parent component
Accessing values
Using this.state property
Using this.props property
Changing values
Only within the component
Only changed by the parent component
Sharing data
Not shared between components
Can be shared between multiple components using the same prop
There are no rows in this table
React state is used to manage and control the internal state of a component. It is mutable and can only be changed using the setState() method within the component. The this.state property is used to access the values of the state. State is not shared between components and is only accessible within the component it belongs to.
On the other hand, React props are immutable and passed down from the parent component to the child component. The this.props property is used to access the values of the props. A child component cannot change the values of the props directly; only the parent component can do so. Props can be shared between multiple components using the same prop.
To illustrate how to create a table component in React using React state and props, we can refer to an example provided by LogRocket [
]. In this example, the table component uses state to control the sorting and filtering of data within the table. Props are used to pass the table data and column configuration from the parent component to the table component.
It is important to note that when using React state, caution should be taken not to mutate the state directly as it can lead to unpredictable behavior. Instead, the setState() method should be used to update the state. Similarly, when using React props, it is recommended to use a one-way data flow where the parent component passes the props down to the child component to ensure a clear and predictable data flow [
In summary, React state and props are both crucial concepts in building reusable and dynamic components in React. While state is used to manage and control the internal state of a component, props are used to pass data from a parent component to a child component. It is important to understand their similarities and differences to effectively use them in React development.

Simple comparative code examples of React Hello World apps showcasing the use of state and props:
Using Props:
import React from 'react';

function HelloWorld(props) {
return <h1>Hello {}!</h1>;

export default HelloWorld;

import React from 'react';
import HelloWorld from './HelloWorld';

function App() {
return (
<HelloWorld name="John" />
<HelloWorld name="Jane" />

export default App;

In this example, we define a HelloWorld component that takes a name prop and renders a greeting message with the provided name. We then use this component in the App component and pass different name props to create two different greeting messages. [

Example using STATE:
Here's the complete code for a React Native app with a counter that increments on button click:
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

function Counter() {
const [count, setCount] = useState(0);

const incrementCount = () => {
setCount(count + 1);

return (
<Text>You clicked the button {count} times.</Text>
<Button title="Click me!" onPress={incrementCount} />

export default function App() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Counter />

In this code, we import useState from react and View, Text, and Button from react-native. We then define a Counter component that uses the useState hook to keep track of a count and a Button component to trigger an increment of the count. Finally, we define the main App component, which renders the Counter component within a View component that centers the content vertically and horizontally within the app.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.