Code Monkey home page Code Monkey logo

phase-2-hooks-workshop-custom-hooks's Introduction

Custom Hooks Workshop

Learning Goals

  • Understand the benefits of creating custom hooks
  • Create custom hooks

Introduction

This workshop is designed to walk you through a series of short exercises led by an instructor where you'll be introduced to some new concepts, experiment with code, and go over a solution. The setup for this workshop is different from a typical lab, so make sure to read all the steps below before getting started!

Workshop Videos

This series of videos walks through the setup for each exercise, as well as going through a solution. Check out these videos if you're working on this workshop outside of a lecture setting:

You can view the solution code for this playlist on the solution branch.

Setup

  • Fork and clone this repo
  • Run npm install in the repo directory
  • Run npm start to run the project in the browser
  • In another terminal, run npm test to run the tests for all exercises

In the tab that is running tests, you can press the p key to select a specific test file to run. For example, hitting p and then typing 01.js will run the first test.

NOTE: if you get the following error when running npm start or npm test:

Module not found: Error: Can't resolve '@styled-icons/heroicons-outline/ExternalLink'

In VS Code, navigate to the following file in the node_modules folder:

node_modules/@ihollander/workshop-app/dist/components/sandbox.js

Then comment out the following line of code (line 22):

var _ExternalLink = require("@styled-icons/heroicons-outline/ExternalLink");

Instructions

The src directory has two folders: one for the solution code, and one for the exercises. You'll be working in the exercise folder.

  • src/exercise/01.js: Exercise (write your code here)
  • src/exercise/01.md: Deliverables and helpful notes
  • src/__tests__/01.js: Exercise test
  • src/__tests__/01.extra-1.js: Test for extra credit exercise
  • src/solution/01.js: Solution code (check your work, or look for a hint if you're stuck)
  • src/solution/01.extra-1.js: Solution code for extra credit exercise

Each deliverable in the exercise folder has comments to guide your work!

There are some emoji to guide you in the exercises:

  • โœ… Instructions where to write your code
  • ๐Ÿ‘€ A hint on what syntax to use/what code to write
  • ๐Ÿ“ƒ Helpful documentation

All credit to Kent C Dodds for the emoji guide idea, and general inspiration for this workshop format!

When running the workshop app in the browser, you can see the readme for each exercise alongside a sandbox where you can view your exercise code to see if it works. You can also open up your exercise code in a new browser tab to view the exercise alone, and more easily see what's happening in the React Dev Tools.

You can also view working examples of the code in the browser by clicking the solution tab.

Some exercises have bonus challenges to do for extra credit, so if you finish early, give them a shot! Or save them for later when you want to revisit these exercises.

phase-2-hooks-workshop-custom-hooks's People

Contributors

hansenjl avatar ihollander avatar jlboba avatar lizbur10 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-2-hooks-workshop-custom-hooks's Issues

Custom Hooks Workshop Lab...External Link doesn't exist

Canvas Link

https://learning.flatironschool.com/courses/6199/assignments/229261?module_item_id=536166

Concern

When running npm start, the web application cannot compile because External Link doesn't exist within the path noted in the error.
Compiled with problems:X

ERROR in ./node_modules/@ihollander/workshop-app/dist/components/sandbox.js 14:20-75

Module not found: Error: Can't resolve '@styled-icons/heroicons-outline/ExternalLink' in '/Users/laurenhalpert/Development/code/phase-2/phase-2-hooks-workshop-custom-hooks/node_modules/@ihollander/workshop-app/dist/components'

Additional Context

Got help from a TC and together we determined the lab is broken.

Suggested Changes

modify lab so External Link exists or eliminate External Link

phase-2-hooks-workshop-custom-hooks | 01.test.js Always Fails

Canvas Link

https://my.learn.co/courses/590/assignments/27694?module_item_id=68851

Concern

In the 01test.js file, whenever I run the code after creating the useDocumentTittle Hook the test fails. Even after using the code from the solution folder it still fails. The problem here is with the test file 01.test.js and how this test is written, after doing some research I found that the issue is with this code snippet

  test("sets the document title", () => {
    renderHook(() => useDocumentTitle());
    act(() => {
      expect(document.title).toBe("Welcome to the home page!");
    });
  });
});

The issue seems to be that the document.title is not being updated synchronously in the test environment. The test is asserting the value immediately after rendering the hook, but the document.title may not have been updated yet.

Additional Context

This test keeps failing and all the code I tried including the solution folder code does not seem to work. I keep getting this error:
here is the code I wrote:

import { useEffect } from "react";
export function useDocumentTitle(title) {
  useEffect(() => {
    document.title = title;
  }, [title]);
}

export default function Home() {
  useDocumentTitle("testing");
  return (
    <div>
      <h1>Home Page</h1>
      <p>
        To see the title change in the browser tab, click the 'Open in new tab'
        link above
      </p>
    </div>
  );
}

and here is the test results:

` FAIL src/tests/01.test.js
Exercise 01
โœ“ is exported as a named export (2 ms)
โœ• sets the document title (15 ms)

โ— Exercise 01 โ€บ sets the document title

expect(received).toBe(expected) // Object.is equality

Expected: "Welcome to the home page!"
Received: "undefined"

  15 |     renderHook(() => useDocumentTitle());
  16 |     act(() => {
> 17 |       expect(document.title).toBe("Welcome to the home page!");
     |                              ^
  18 |     });
  19 |   });
  20 | });

  at src/__tests__/01.test.js:17:30
  at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:22380:12)
  at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1042:14)
  at Object.<anonymous> (src/__tests__/01.test.js:16:5)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.484 s, estimated 1 s
Ran all test suites matching /src/tests/01.test.js/i.`

Suggested Changes

To address this, we can use a trick with setTimeout and a callback to wait for the title to be updated before asserting its value. Here's how we can re-write it:

  test("sets the document title", () => {
    renderHook(() => useDocumentTitle());
    act(() => {
      setTimeout(() => {
        expect(document.title).toBe("Welcome to the home page!");
      }, 0);
    });
  });

I tested this new code and now all the tests are passing. Hope this helps!

Required Phase 2 Lab Or WorkShop for Custom Hooks DOES NOT COMPILE INITIALLY EVEN WITH INSTRUCTIONS

Canvas Link

https://learning.flatironschool.com/courses/6558/assignments/242839?module_item_id=571446

Concern

Forked and cloned the repo, changed into it, and ran npm install which succeeded. But just as the lab stated, on npm start, ran into an error. npm test said there were no tests which I found strange since it is to be submitted to CodeGrade.

Failed to compile.

Module not found: Error: Can't resolve '@styled-icons/heroicons-outline/ExternalLink' in '/home/chrisubuntu/Development/phase2/phase-2-hooks-workshop-custom-hooks/node_modules/@ihollander/workshop-app/dist/components'
ERROR in ./node_modules/@ihollander/workshop-app/dist/components/sandbox.js 14:20-75
Module not found: Error: Can't resolve '@styled-icons/heroicons-outline/ExternalLink' in '/home/chrisubuntu/Development/phase2/phase-2-hooks-workshop-custom-hooks/node_modules/@ihollander/workshop-app/dist/components'

webpack compiled with 1 error

Tried changing the files, but either I don't understand how to modify said file to not require the module at all or for some reason it refused to acknowledge any changes to said file.

Additional Context

On Discord it was suggested that when I installed react-router version 5 after running: "npm install -g npm"
npm install react-router-dom@5

It was suggested that that was the cause of said problem.

Suggested Changes

Do not make this lab required and instead redesign it so it does not use those out of date unsupported libraries and require the new one instead. But no solution to said problem was adequately provided and since it uses the _ExternalLink in not just one, but 3 places, your repo should also remove those.

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.