State And Props in React & React Native
A Complete Guide — Building Blocks of Dynamic UIs 🏗️

Fundamentals 📚
What is a State🤔
In React, state is like a container that holds dynamic data, specific to a single component. It enables your component to maintain and display information that can change over time, such as user input or API responses. The key idea behind state is that it’s “mutable”, meaning you can change its value during the component’s lifecycle.
What are Props 🤔
Props, short for properties, are the means by which data is passed from one component to another in React and React Native. They are read-only and serve as a way for parent components to communicate with child components. Props can be thought of as the “immutable” counterpart to state.
State: The Dynamic Core🔄
Defining and Initializing State🏁
In React, you typically define state in class components using the constructor
method, while functional components use the useState
hook. It's essential to initialize state with an initial value to ensure your component starts with the correct data.
class ClassComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
}
function FunctionalComponent() {
const [count, setCount] = useState(0);
}
Updating State🔁
Changing state is at the heart of creating dynamic user interfaces. To update state, use the setState
method in class components and the updater function returned by the useState
hook in functional components.
this.setState({ count: this.state.count + 1 }); // Class component
setCount(count + 1); // Functional component
State in Functional Components ⚙️
With the introduction of hooks, functional components can now also manage state. We use the useState
hook to declare and manage state variables within functional components, making them more versatile and concise.
const [count, setCount] = useState(0);
Props: The Messengers of Data 💬
Passing Data with Props📤
Passing data from parent to child components is done by declaring props in the child component’s function signature. You can then access these props as properties of the component.
function ChildComponent(props) {
return <Text>{props.message}</Text>;
}
<ChildComponent message="Hello, World!" />
Default Props🔧
You can specify default values for props in case they are not provided when the component is used. This ensures your component behaves predictably.
function ChildComponent(props) {
return <Text>{props.message}</Text>;
}
ChildComponent.defaultProps = {
message: 'Default Message',
};
Remember that props are immutable. Once a parent component passes a prop to a child, the child cannot modify it directly. If you need to change a value, you should do so in the parent component, and React will handle the rest.

Distinguishing State and Props ⚖️
State vs. Props: A Side-by-Side Comparison
State is for managing a component’s private data that can change over time, while props are for passing data from parent to child components. Here’s a quick comparison:
State:
Mutable
Managed internally by a component.
Updated within the component
Requires using
setState
to trigger a re-render and update the component.Used to manage and update a component’s internal data and state.
Props:
Immutable
Passed from parent to child 👨👧
Read-only in the child component
Automatically updates when the parent component passes new props.
Used for communication between components (Parent to child)
When to Use State vs. Props: If a piece of data should only affect the component itself and is subject to change, use state. On the other hand, if data needs to be shared between components, pass it as props.

Real-World Applications🌐
Building Interactive Forms with State📝
State is invaluable when creating forms in React Native. Each input field’s value can be stored in the component’s state, allowing you to handle user input and validation effectively.
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
Customizing Components with Props🎨
Props provide a way to customize and reuse components in your app. For example, you can pass different text or styles to a button component to adapt its appearance and functionality.
<Button label="Submit" color="blue" />
<Button label="Cancel" color="red" />
In complex UIs, it’s common to combine both state and props. State handles user interactions and dynamic data, while props ensure data consistency across different parts of your app.
Handling Events and User Interaction👥
Event Handling with State🎯
Managing user interactions often involves changing the component’s state. For example, handling a button click event to increment a counter.
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<View>
<Text>{count}</Text>
<Button title="Increment" onPress={increment} />
</View>
);
}
Event Handling with Props📆
Props can also be used to handle events. You can pass event handlers from a parent component to a child component as props.
function ParentComponent() {
const handleButtonClick = () => {
// Handle the button click here
};
return <ChildComponent onClick={handleButtonClick} />;
}
function ChildComponent(props) {
return <Button title="Click me" onPress={props.onClick} />;
}

Managing Multiple Components🧩
Passing Props Between Parent and Child Components
React Native applications often consist of multiple components working together. Passing props from parent to child components helps maintain a clean and organized code structure.
function ParentComponent() {
const data = "Hello from Parent";
return <ChildComponent data={data} />;
}
function ChildComponent(props) {
return <Text>{props.data}</Text>;
}
In some cases, it’s necessary to lift state up to a common ancestor when multiple child components need access to the same data. This ensures data consistency across the application.
Common Mistakes and How to Avoid Them⚠️
Mutating State and Props🚫
Directly altering state or props is one of the most common mistakes. To update state, always utilize the provided methods (e.g., setState or state hooks) and treat props as read-only.
Effective Debugging with State and Props🐞
When dealing with state and props-related issues, React DevTools are your best friends. They allow you to inspect and debug the component tree, view state changes, and trace the flow of props.

Inefficient Re-renders⏳
Avoid unnecessary re-renders by optimizing your components using techniques like shouldComponentUpdate
or React.memo
for functional components. This ensures your app stays responsive and performant.
Conclusion🤝
Mastering the concepts of state and props in React is a significant step toward becoming a proficient React developer. These fundamental building blocks are the keys to creating dynamic and interactive user interfaces. So, whether you’re building a simple to-do list app or a complex E-commerce platform, remember that state and props are your allies on the journey to crafting exceptional mobile and web experiences.
Happy Coding 🤞
