Git Product home page Git Product logo

8-0-react-hooks-lab's Introduction

React Classes to Hooks Conversion Lab

In this lab, you will build a simple page that will show a simple sign up page that also access an external API. The goals are:

  • Rewrite all the components from class components
  • Add hooks
  • Change state using hooks
  • Use useEffect as the lifecycle method for updating a component after an API call

Each of the sections below (in the gold border) is its own component. Focus on completing the distinct components. There is not an overall "form" or "feature" to complete.

Example of a completed application.


Lab Setup

Getting started

  1. Fork and clone this repository.

  2. Navigate to the cloned repository's directory on your command line. Then, run the following command:

    npm install
    

    This will install the libraries needed to run the tests.

  3. Open up the repository in VSCode. Follow the instructions below to complete the Lab.

Tests

To run the tests, you can run the following command from the command line. You will need to be in the root directory of your local directory.

npm test

This will open the Cypress testing window, where you can click to run an individual suite of tests or all of the tests at once.

Testing Tips

Keep the following in mind for this lab as you run the tests.

  1. While running your tests, you must have a server up and running in another terminal. This means you will have both a terminal window running the actual React application and a terminal window running the tests.

  2. When creating a component, make sure to create and import it with the same name as the file name. For example, the component created and exported inside of the NavBar.js file should be NavBar. The tests look for these specific names.

  3. While the cypress-watch-and-reload package has been installed in this project, sometimes the React application will take longer to reload than the tests. If you feel as though a test should be passing that isn't, try pressing the re-run button in the Cypress tests before asking for help.

Hooks vs React Stateful Class Components

If you've worked with array classes you would write

// Declare state, where you can add more properties
// Set default value of this.state.bookmarks to be an empty array
this.state = {
  bookmarks: []
}

updateBookmarks () {
  // Do some stuff
  // ...
  // Update state:
  // use generic this.setState() function, inside the function set which property will be updated
  this.setState({bookmarks: ["This array is updated"]})
}

With hooks, it does the same thing, but in a cleaner, more readable way.

// Declare state for bookmarks only. If you want to add more properties, you would create a new line and call useState() again.
// Set default value bookmarks to be an empty array
// Set the name of the funciton that will be in charge of updating bookmarks
const [bookmarks, setBookmarks] = useState([]);

updateBookmarks() {
  // Do some stuff
  // ...
  // Update state:
  // Use the function you created and named to update just bookmarks, if you have other properties to update, you would call their specific functions as well
  setBookmarks(["This array is updated"]);
}

Instructions

  • NavBar - convert to functional component
  • Footer - convert to functional component
  • FirstPet - [Work with a Boolean]
    • Convert to functional component
    • Use a checkbox to update state of firstPet to have a value true or false
    • Use conditional rendering to render the word No if the checkbox is unchecked
    • Use conditional rendering to render the word Yes if the checkbox is checked
  • NumOfPets - [Work with numbers, work with multiple functions]
    • Convert to a functional component
    • Add two buttons one that says - and one that says +
    • On click of + increase the number of pets by 1
    • On click of - decrease the number of pets by 1, Do not allow for a negative amount of pets
  • NewClient - [Work with strings, work with multiple stateful properties]
    • Convert to functional component
    • Make sure your form contains:
      • firstName, type "text"
      • lastName, type "text"
      • phone, type "tel"
      • email, type "email"
    • Render the values in an article alongside the form as you type
  • AnimalTypes - [Work with arrays]
    • Convert to functional component
    • Be able to render a list of animals (already provided)
    • Be able to add to the list using a form and a submit input,
    • Do not allow for duplicates
    • Make sure that cat and cat are all entered as cat
    • Be able to remove an animal with an on click of a button
  • PetList - [Work with an external API, lifecycle method, pass props down to Pet component]
    • Make an API call to receive all pets (describe below)
    • Be able to render a list of the animals including their
      • name
      • breed
      • kind

Using the API

Your instructor will either give you the URL for the API you will be accessing or tell you to run the API locally. There will be only one endpoint you will need to hit.

If you need, you can access the API at the following repository. Follow the instructions there to get it installed.

/api/pets

Making a GET request to this path will return an array of pets. The response will look similar to the one below.

[
  {
    id: "dGXf5O9",
    name: "Lady",
    kind: "Dog",
    breed: "Doberman Pinscher",
    employeeId: "z7GIN_i",
  },
  // ...
];

8-0-react-hooks-lab's People

Contributors

bwreid avatar derekpeterson-plutus avatar krafalski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.