This demo illustrates the concept of lifting state up, a key pattern for managing and sharing state between components in React.
In this application:
- Items are added through the
<Form>
component. - Items are displayed in the
<PackagingList>
component.
Because the <Form>
and <PackagingList>
components are siblings—they do not share a direct parent-child relationship—their shared state must be lifted up to their closest common ancestor, which in this case is the <App>
component. This allows the two components to communicate and share data via a common parent.
According to React’s official design principles, components should manage their own state if they can, but when two or more components need to share the same state, it should be lifted up to their closest common parent. In this app, the shared state (the list of items) is maintained in the parent <App>
component.
This makes the unidirectional data flow possible:
- State is passed down to the child components (
<Form>
and<PackagingList>
) via props. - Events (such as adding an item) are passed up via callback functions, allowing the child components to notify the parent of state changes.
- State: The list of items is stored in the parent component
<App>
. React'suseState
hook is used to manage this local state. - Props: The state (the items) is passed down to the
<PackagingList>
as props, allowing the list to be rendered. The<Form>
component also receives a function prop to update the state when new items are added.
In React, data flows in one direction: from parent to child. This concept simplifies the logic of data handling and ensures that changes in the application state are predictable. In this app:
- The state is managed in the parent
<App>
component. - The
<Form>
component updates the parent state by passing new data through a function. - The
<PackagingList>
component renders the list based on the current state received as props from the parent.
This architecture is what makes React's data flow declarative: components declare how the UI should look based on the state they receive.
-
The
<Form>
component allows the user to input data (an item description and quantity) and submit it. Upon form submission, theonAddItems
function is invoked, which is passed down from<App>
. This function updates the state in the parent. -
The
<App>
component’s state is updated with the new item, and React re-renders the component tree. -
The
<PackagingList>
component, which receives the updated items list via props, automatically re-renders to display the new item.
- Parent-to-Child: The updated list of items is passed from
<App>
to<PackagingList>
through props. - Child-to-Parent: The
<Form>
component sends data up to the<App>
component by invoking the callback function provided via props.
Lifting state up is a fundamental pattern in React that helps maintain the single source of truth for any given state in the component tree. When multiple components need to share state, lifting it up to the nearest common parent avoids inconsistencies and ensures that all components stay synchronized.
This approach also maintains React’s principle of unidirectional data flow, ensuring that data is passed down through the component hierarchy while state changes and events are bubbled up through callback functions.