Reader small image

You're reading from  Learn React with TypeScript - Second Edition

Product typeBook
Published inMar 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781804614204
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Carl Rippon
Carl Rippon
author image
Carl Rippon

Carl Rippon has been in the software industry for over 20 years developing a complex lines of business applications in various sectors. He has spent the last 8 years building single-page applications using a wide range of JavaScript technologies including Angular, ReactJS, and TypeScript. Carl has also written over 100 blog posts on various technologies.
Read more about Carl Rippon

Right arrow

Questions

Answer the following questions to reinforce what you have learned in this chapter:

  1. What is wrong with the following component definition?
    export function important() {
      return <div>This is really important!</div>;
    }
  2. A component with a prop is defined as follows:
    export function Name({ name }) {
      return <div>name</div>;
    }

The value of the prop isn’t output though. What is the problem?

  1. Component props are passed into a component as follows:
    <ContactDetails name="Fred" email="fred@somewhere.com" />

The component is then defined as follows:

export function ContactDetails({ firstName, email }) {
  return (
    <div>
      <div>{firstName}</div>
      <div>{email}</div>
    </div>
  );
}

The name Fred isn’t output though. What is the problem?

  1. What is wrong with how the click event is handled in the following JSX:
    <button click={() => console.log("clicked")}>
      Click me
    </button>;
  2. What is the initial value of the loading state defined here?
    const [loading, setLoading] = useState(true);
  3. What is wrong with how the state is set in the following component?
    export function Agree() {
      const [agree, setAgree] = useState();
      return (
        <button onClick={() => agree = true}>
          Click to agree
        </button>
      );
    }
  4. The following component implements an optional Agree event. What is wrong with this implementation?
    export function Agree({ onAgree }) {
      function handleClick() {
        onAgree();
      }
      return (
        <button onClick={handleClick}>
          Click to agree
        </button>
      );
    }
Previous PageNext Page
You have been reading a chapter from
Learn React with TypeScript - Second Edition
Published in: Mar 2023Publisher: PacktISBN-13: 9781804614204
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Carl Rippon

Carl Rippon has been in the software industry for over 20 years developing a complex lines of business applications in various sectors. He has spent the last 8 years building single-page applications using a wide range of JavaScript technologies including Angular, ReactJS, and TypeScript. Carl has also written over 100 blog posts on various technologies.
Read more about Carl Rippon