React, a popular JavaScript library for building user interfaces, uses two main concepts to manage and pass data within components: Props and State. These concepts are pivotal in creating dynamic and interactive web applications. In this blog post, we'll dive deep into both Props and State, explaining their roles, differences, and how to use them effectively.
Props, short for properties, are a way of passing data from parent components to child components in React. They are read-only, which means they cannot be modified by the child components that receive them. Props are primarily used to render dynamic data (Dynamic data can include anything that might need to be displayed in the UI and could change based on user interaction or network responses—anything from text in a blog post to user profile details) and trigger state changes in other components.
Props are used to configure components in various ways when they are being created.
By using props to pass data, you can make your components reusable by giving different data inputs.
Props => Unidirectional Dataflow. i.e, Parent to Child.
function ParentComponent() {
return <ChildComponent greet="Hello, World!" />;
}
function ChildComponent(props) {
return <h1>{props.greet}</h1>;
}
ChildComponent.defaultProps = {
greet: "Hello, Default!"
};
State refers to a set of data that can change overtime, usually in response to user actions or events (like clicks, from inputs).
State is managed within the component (similar to variables declared within a function).
When the state of a component changes, the component re-renders to reflect the changes.
Mutable: can be changed using `setState()` method in Class Components or `useState()` hook in Functional Components
Component Specific: State is local to the component where it is declared or can be raised to parent level when multiple children need to access it.
useState
hook in functional components.
class ExampleComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
}
function ExampleComponent() {
const [count, setCount] = useState(0);
}
setState
in class components or the updater function from useState
in functional components.
this.setState({ count: this.state.count + 1 });
setCount(count + 1);
Feature | Props | State |
---|---|---|
Origin | Received from parent | Defined inside component |
Mutability | Immutable (read-only) | Mutable (can change) |
Use | Pass data and functions | Handle changes in component |
Prop Drilling: Sometimes, you need to pass props through multiple layers of components. This can be cumbersome and make your components tightly coupled. To solve this, consider using React context or state management libraries like Redux or MobX for deeper component trees.
Component Reusability: Make components as independent as possible. Use props to pass data and avoid relying too much on the global state.
State Updates Are Asynchronous: Always be aware that state updates might not apply immediately. For operations depending on the previous state, use the functional form of setState or the updater function.