Pie Logo
Quiz: Props
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
JSX and Components
Props
Exercise: Props
Quiz: Props
Assignment 1.2: Front End
CSS Styles
useState and Hooks
Conditional Rendering
Lists
Forms and Events
Storybook
Assignment 1.3: Bonus - Backend
Leaderboards
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: Props
    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
    JSX and Components
    Props
    Exercise: Props
    Quiz: Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    Props Quiz
    by Tomas Trescak· Props

    QUESTION 1

    You’re building a reusable Card component for a blog website. The component needs to display a title and some content, but the content could be a paragraph, a list, or even an image. Here’s the code:

    import React from 'react';
    type CardProps = {
      title: string;
      content: React.ReactNode;
    };
    const Card = (props: CardProps) => (
      <div>
        <h2>{props.title}</h2>
        <div>{props.content}</div>
      </div>
    );
    const App = () => (
      <div>
        <Card title="First Post" content={<p>This is a blog post.</p>} />
        <Card title="To-Do List" content={<ul><li>Task 1</li></ul>} />
      </div>
    );
    

    What is the benefit of using React.ReactNode as the type for the content prop?


    QUESTION 2

    Consider this code where a Profile component displays a user’s name and an optional bio:

    type ProfileProps = {
      name: string;
      bio?: string;
    };
    const Profile = ({ name, bio = "No bio provided" }) => (
      <div>
        <h1>{name}</h1>
        <p>{bio}</p>
      </div>
    );
    const App = () => (
      <div>
        <Profile name="Alice" />
        <Profile name="Bob" bio="Loves coding!" />
      </div>
    );
    

    What will be displayed when <Profile name="Alice" /> is rendered?


    QUESTION 3

    You’re debugging a teammate’s code and notice they tried to modify a prop directly:

    type DisplayProps = {
      count: number;
    };
    const Display = (props: DisplayProps) => {
      props.count = props.count + 1; // Attempting to modify prop
      return <div>Count: {props.count}</div>;
    };
    const App = () => <Display count={5} />;
    

    What will happen when this code runs in a development environment?


    QUESTION 4

    Look at this code using destructuring and a boolean prop:

    type UserProps = {
      username: string;
      isActive: boolean;
    };
    const User = ({ username, isActive }) => (
      <div>
        {username} - {isActive ? "Online" : "Offline"}
      </div>
    );
    const App = () => (
      <div>
        <User username="Charlie" isActive />
        <User username="Dana" isActive={false} />
      </div>
    );
    

    What will be rendered for <User username="Charlie" isActive />?


    QUESTION 5

    Imagine you’re creating a Section component that wraps content and passes props to an inner component:

    type SectionProps = {
      title: string;
      highlight: boolean;
    };
    const InnerSection = ({ title, highlight }) => (
      <h1 style={{ color: highlight ? "blue" : "black" }}>{title}</h1>
    );
    const Section = (props) => (
      <div>
        <InnerSection {...props} />
        <p>Some content here</p>
      </div>
    );
    const App = () => <Section title="Welcome" highlight />;
    

    What does the {...props} syntax do in this code?

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

    Props Quiz
    by Tomas Trescak· Props

    QUESTION 1

    You’re building a reusable Card component for a blog website. The component needs to display a title and some content, but the content could be a paragraph, a list, or even an image. Here’s the code:

    import React from 'react';
    type CardProps = {
      title: string;
      content: React.ReactNode;
    };
    const Card = (props: CardProps) => (
      <div>
        <h2>{props.title}</h2>
        <div>{props.content}</div>
      </div>
    );
    const App = () => (
      <div>
        <Card title="First Post" content={<p>This is a blog post.</p>} />
        <Card title="To-Do List" content={<ul><li>Task 1</li></ul>} />
      </div>
    );
    

    What is the benefit of using React.ReactNode as the type for the content prop?


    QUESTION 2

    Consider this code where a Profile component displays a user’s name and an optional bio:

    type ProfileProps = {
      name: string;
      bio?: string;
    };
    const Profile = ({ name, bio = "No bio provided" }) => (
      <div>
        <h1>{name}</h1>
        <p>{bio}</p>
      </div>
    );
    const App = () => (
      <div>
        <Profile name="Alice" />
        <Profile name="Bob" bio="Loves coding!" />
      </div>
    );
    

    What will be displayed when <Profile name="Alice" /> is rendered?


    QUESTION 3

    You’re debugging a teammate’s code and notice they tried to modify a prop directly:

    type DisplayProps = {
      count: number;
    };
    const Display = (props: DisplayProps) => {
      props.count = props.count + 1; // Attempting to modify prop
      return <div>Count: {props.count}</div>;
    };
    const App = () => <Display count={5} />;
    

    What will happen when this code runs in a development environment?


    QUESTION 4

    Look at this code using destructuring and a boolean prop:

    type UserProps = {
      username: string;
      isActive: boolean;
    };
    const User = ({ username, isActive }) => (
      <div>
        {username} - {isActive ? "Online" : "Offline"}
      </div>
    );
    const App = () => (
      <div>
        <User username="Charlie" isActive />
        <User username="Dana" isActive={false} />
      </div>
    );
    

    What will be rendered for <User username="Charlie" isActive />?


    QUESTION 5

    Imagine you’re creating a Section component that wraps content and passes props to an inner component:

    type SectionProps = {
      title: string;
      highlight: boolean;
    };
    const InnerSection = ({ title, highlight }) => (
      <h1 style={{ color: highlight ? "blue" : "black" }}>{title}</h1>
    );
    const Section = (props) => (
      <div>
        <InnerSection {...props} />
        <p>Some content here</p>
      </div>
    );
    const App = () => <Section title="Welcome" highlight />;
    

    What does the {...props} syntax do in this code?

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

    Pie Logo
    Quiz: Props
    by Tomas Trescak
    Pie Logo
    Quiz: Props
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Exercise: Props
    Quiz: Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    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
    JSX and Components
    Props
    Exercise: Props
    Quiz: Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics