Pie Logo
Quiz: Pure Components
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
Next.js
Advanced React
Pure Components - memo
Quiz: Pure Components
👾 Pure Components
Lifecycle - useEffect
Expensive? - useMemo
DOM Interactions - useRef
Error Boundaries
Context
Leaderboards
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: Pure Components
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Quiz: Pure Components
    👾 Pure Components
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    Pure Components
    by Tomas Trescak· Pure Components - memo

    QUESTION 1

    You’re building a dashboard app with a UserList component that renders a list of 1,000 users. The parent component, Dashboard, has a state filter that changes when the user types in a search bar. Here’s the code:

    import React from 'react';
    const UserList = ({ users }) => {
      console.log('UserList rendered');
      return (
        <ul>
          {users.map(user => (
            <li key={user.id}>{user.name}</li>
          ))}
        </ul>
      );
    };
    const Dashboard = () => {
      const [filter, setFilter] = React.useState('');
      const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; // Imagine 1,000 users
      return (
        <div>
          <input value={filter} onChange={e => setFilter(e.target.value)} />
          <UserList users={users} />
        </div>
      );
    };
    export default Dashboard;
    

    Every time the user types in the input field, UserList re-renders, even though the users prop doesn’t change. Which of the following could help optimize this?


    QUESTION 2

    Consider this code where a Timer component displays a static message, but it re-renders every time the parent’s count changes:

    import React from 'react';
    const Timer = ({ message }) => {
      console.log('Timer rendered');
      return <div>{message}</div>;
    };
    const App = () => {
      const [count, setCount] = React.useState(0);
      return (
        <div>
          <button onClick={() => setCount(count + 1)}>Count: {count}</button>
          <Timer message="Time is ticking!" />
        </div>
      );
    };
    

    You wrap Timer with React.memo. What happens when you click the button?


    QUESTION 3

    Imagine you’re optimizing a large e-commerce app. The ProductCard component displays product details and is used in a ProductList. Here’s a simplified version:

    import React from 'react';
    const ProductCard = ({ product }) => {
      console.log(`ProductCard ${product.id} rendered`);
      return <div>{product.name} - ${product.price}</div>;
    };
    const ProductList = () => {
      const [cart, setCart] = React.useState([]);
      const products = [
        { id: 1, name: 'Laptop', price: 999 },
        { id: 2, name: 'Phone', price: 599 },
      ];
      return (
        <div>
          <button onClick={() => setCart([...cart, products[0]])}>
            Add Laptop to Cart
          </button>
          {products.map(product => (
            <ProductCard key={product.id} product={product} />
          ))}
        </div>
      );
    };
    

    When you click "Add Laptop to Cart," all ProductCard components re-render. How can you optimize this?


    QUESTION 4

    You’re debugging a slow app and notice a component re-renders unnecessarily. Here’s the code:

    import React from 'react';
    const Display = React.memo(({ value }) => {
      console.log('Display rendered');
      return <div>{value}</div>;
    });
    const Counter = () => {
      const [count, setCount] = React.useState(0);
      const value = { number: 42 }; // Object created anew each render
      return (
        <div>
          <button onClick={() => setCount(count + 1)}>Count: {count}</button>
          <Display value={value} />
        </div>
      );
    };
    

    Even with React.memo, Display re-renders every time count changes. Why, and how can you fix it?


    QUESTION 5

    In a real-world app, you’re rendering a list of comments. Each Comment component is wrapped with React.memo, but some still re-render unnecessarily. What might be causing this?

    This content is only available to registered users.
    Please register at the the home page.

    Pure Components
    by Tomas Trescak· Pure Components - memo

    QUESTION 1

    You’re building a dashboard app with a UserList component that renders a list of 1,000 users. The parent component, Dashboard, has a state filter that changes when the user types in a search bar. Here’s the code:

    import React from 'react';
    const UserList = ({ users }) => {
      console.log('UserList rendered');
      return (
        <ul>
          {users.map(user => (
            <li key={user.id}>{user.name}</li>
          ))}
        </ul>
      );
    };
    const Dashboard = () => {
      const [filter, setFilter] = React.useState('');
      const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; // Imagine 1,000 users
      return (
        <div>
          <input value={filter} onChange={e => setFilter(e.target.value)} />
          <UserList users={users} />
        </div>
      );
    };
    export default Dashboard;
    

    Every time the user types in the input field, UserList re-renders, even though the users prop doesn’t change. Which of the following could help optimize this?


    QUESTION 2

    Consider this code where a Timer component displays a static message, but it re-renders every time the parent’s count changes:

    import React from 'react';
    const Timer = ({ message }) => {
      console.log('Timer rendered');
      return <div>{message}</div>;
    };
    const App = () => {
      const [count, setCount] = React.useState(0);
      return (
        <div>
          <button onClick={() => setCount(count + 1)}>Count: {count}</button>
          <Timer message="Time is ticking!" />
        </div>
      );
    };
    

    You wrap Timer with React.memo. What happens when you click the button?


    QUESTION 3

    Imagine you’re optimizing a large e-commerce app. The ProductCard component displays product details and is used in a ProductList. Here’s a simplified version:

    import React from 'react';
    const ProductCard = ({ product }) => {
      console.log(`ProductCard ${product.id} rendered`);
      return <div>{product.name} - ${product.price}</div>;
    };
    const ProductList = () => {
      const [cart, setCart] = React.useState([]);
      const products = [
        { id: 1, name: 'Laptop', price: 999 },
        { id: 2, name: 'Phone', price: 599 },
      ];
      return (
        <div>
          <button onClick={() => setCart([...cart, products[0]])}>
            Add Laptop to Cart
          </button>
          {products.map(product => (
            <ProductCard key={product.id} product={product} />
          ))}
        </div>
      );
    };
    

    When you click "Add Laptop to Cart," all ProductCard components re-render. How can you optimize this?


    QUESTION 4

    You’re debugging a slow app and notice a component re-renders unnecessarily. Here’s the code:

    import React from 'react';
    const Display = React.memo(({ value }) => {
      console.log('Display rendered');
      return <div>{value}</div>;
    });
    const Counter = () => {
      const [count, setCount] = React.useState(0);
      const value = { number: 42 }; // Object created anew each render
      return (
        <div>
          <button onClick={() => setCount(count + 1)}>Count: {count}</button>
          <Display value={value} />
        </div>
      );
    };
    

    Even with React.memo, Display re-renders every time count changes. Why, and how can you fix it?


    QUESTION 5

    In a real-world app, you’re rendering a list of comments. Each Comment component is wrapped with React.memo, but some still re-render unnecessarily. What might be causing this?

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: Pure Components
    by Tomas Trescak
    Pie Logo
    Quiz: Pure Components
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Quiz: Pure Components
    👾 Pure Components
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Quiz: Pure Components
    👾 Pure Components
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics