Before Making Any Project

    Header
        - Logo
        - NavItems
         - Search
         - Cart
    Body
        - RestaurantList
         - RestaurantCard
           - Image
           - Name
           - Rating
           - Cuisines
    Footer
        - Links
        - Copyright

Let's Talk About JSX

JSX Returns Only One Parent Element

Options:

Wrap in a Single Element

function MyComponent() {
  return (
    <div>
      <h1>Hello, World!</h1>
      <p>This is a paragraph.</p>
    </div>
  );
}

Use React Fragment

function MyComponent() {
  return (
    <>
      <h1>Hello, World!</h1>
      <p>This is a paragraph.</p>
    </>
  );
}

Why Only One Parent?

Can We Use React Fragments Inside React Fragments?

Example:

function NestedFragments() {
  return (
    <>
      <h1>Title</h1>
      <>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
      </>
      <>
        <ul>
          <li>Item 1</li>
          <li>Item 2</li>
        </ul>
      </>
    </>
  );
}

Why Use Nested Fragments?

Here is example:-

function UserProfile({ isLoggedIn, user }) {
  return (
    <>
      <h1>User Profile</h1>
      {isLoggedIn ? (
        <>
          <h2>Welcome, {user.name}!</h2>
          <>
            <p>Email: {user.email}</p>
            <p>Location: {user.location}</p>
          </>
        </>
      ) : (
        <>
          <h2>Please Log In</h2>
          <p>You need to log in to view your profile.</p>
        </>
      )}
    </>
  );
}

// Usage Example:
const user = { name: "John Doe", email: "john@example.com", location: "New York" };

// Logged In
<UserProfile isLoggedIn={true} user={user} />

// Not Logged In
<UserProfile isLoggedIn={false} user={user} />

Note

Config Driven UI

Key Point

Props

Key Concepts

  1. Immutable:
  1. Data Flow:
  1. Functionality

Example

Here's a simple example to illustrate:

// Parent Component
function UserProfile(props) {
  console.log(props); // Logs the props object

  return (
    <div>
      <h1>{props.name}</h1>
      <p>Age: {props.age}</p>
    </div>
  );
}

// Usage in a Parent Component
function App() {
  return <UserProfile name="Alice" age={28} />;
}

Destructuring Props

function UserProfile({ name, age }) {
  return (
    <div>
      <h1>{name}</h1>
      <p>Age: {age}</p>
    </div>
  );
}

Virtual DOM in React

Key Concepts

What is the Virtual DOM?

How It Works:

Benefits

Why Not Use Index as Key in React?

  1. Unstable Keys:
  1. Performance Degradation:
  1. State Management Issues:
  1. Ordering Problems:

Example Problem

function ItemList({ items }) {
  return (
    <ul>
      {items.map((item, index) => (
        <li key={index}>{item.text}</li>
      ))}
    </ul>
  );
}

Issue: If you add an item to the list, all existing items will have their keys (indices) shift.

Prefer Unique Identifiers