Skip to content

trocca/FarAway_ReactDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Far Away React Demo

This demo illustrates the concept of lifting state up, a key pattern for managing and sharing state between components in React.

Overview

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.

Key Concepts

1. Lifting State Up

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.

2. State and Props

  • State: The list of items is stored in the parent component <App>. React's useState 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.

3. Unidirectional Data Flow

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.

How It Works

  1. The <Form> component allows the user to input data (an item description and quantity) and submit it. Upon form submission, the onAddItems function is invoked, which is passed down from <App>. This function updates the state in the parent.

  2. The <App> component’s state is updated with the new item, and React re-renders the component tree.

  3. The <PackagingList> component, which receives the updated items list via props, automatically re-renders to display the new item.

Unidirectional Data Flow in Action:

  • 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.

Why Lifting State Up is Important

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published