Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn React with TypeScript - Second Edition

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

Product type Book
Published in Mar 2023
Publisher Packt
ISBN-13 9781804614204
Pages 474 pages
Edition 2nd Edition
Languages
Author (1):
Carl Rippon Carl Rippon
Profile icon Carl Rippon

Table of Contents (19) Chapters

Preface 1. Part 1: Introduction
2. Chapter 1: Introducing React 3. Chapter 2: Introducing TypeScript 4. Chapter 3: Setting Up React and TypeScript 5. Chapter 4: Using React Hooks 6. Part 2: App Fundamentals
7. Chapter 5: Approaches to Styling React Frontends 8. Chapter 6: Routing with React Router 9. Chapter 7: Working with Forms 10. Part 3: Data
11. Chapter 8: State Management 12. Chapter 9: Interacting with RESTful APIs 13. Chapter 10: Interacting with GraphQL APIs 14. Part 4: Advanced React
15. Chapter 11: Reusable Components 16. Chapter 12: Unit Testing with Jest and React Testing Library 17. Index 18. Other Books You May Enjoy

Answers

Here are the answers to the questions on what you have learned in this chapter:

  1. The problem with the component definition is that its name is lowercase. React functions must be named with an uppercase first character:
    export function Important() {
      ...
    }
  2. The problem is that the name variable inside the div element isn’t enclosed in curly brackets. So, the word name will be output rather than the value of the name prop. Here’s the corrected version of the component:
    export function Name({ name }) {
      return <div>{name}</div>;
    }
  3. The problem is that a name prop is passed rather than firstName. Here’s the corrected JSX:
    <ContactDetails firstName="Fred" email="fred@somewhere.com" />
  4. The problem is that a click prop is passed rather than onClick. Here’s the corrected JSX:
    <button onClick={() => console.log("clicked")}>
      Click me
    </button>;
  5. The initial value of the loading state is true.
  6. The state isn’t updated using the state setter function. Here’s the corrected version of the state being set:
    export function Agree() {
      const [agree, setAgree] = useState();
      return (
        <button onClick={() => setAgree(true)}>
          Click to agree
        </button>
      );
    }
  7. The problem is that clicking the button will cause an error if onAgree isn’t passed because it will be undefined. Here’s the corrected version of the component:
    export function Agree({ onAgree }) {
      function handleClick() {
        if (onAgree) {
          onAgree();
        }
      }
      return (
        <button onClick={handleClick}>
          Click to agree
        </button>
      );
    }
lock icon The rest of the chapter is locked
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.
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 $15.99/month. Cancel anytime}