Giter VIP home page Giter VIP logo

react-component-mounting-and-unmounting-lab's Introduction

React: Component Mounting and Unmounting

Objectives

  1. Describe what happens in the mounting phase of a React component's lifecycle
  2. Describe what happens in the unmounting phase of a React component's lifecycle
  3. Practice performing setup and teardown actions at the appropriate point in the React component lifecycle.

Overview

You are a pancake chef in a fancy pancake restaurant. Time is ticking and customers are waiting, so you better get those pancakes out quick! Pancakes need to be cooked on either side and taken off the pan at the right moment before they burn for optimal deliciousness.

Go on and run the game. It doesn't quite work yet, because some essential bits are missing and it's up to you to finish it off!

Pancakes need to be cooked equally on both sides to be yummy. The current settings are that if a pancake is cooked for exactly 2 seconds on either side, it's considered to be cooked perfectly. If it's cooked for more than 2 seconds on either side it will be burnt and otherwise it will be raw. You can change these settings in <Pancake />!

Shop needs an opening time

First, you want to record the time your shop was opened. Open <Game /> and notice a setCurrentTime() method already exits, but it is not yet called. Create a componentDidMount() method in <Game /> which will call the setCurrentTime() when the component is first created. If you've done that correctly, you should see the current time at the top of the page.

Pancake needs a timer!

Now it's time to implement the actual pancake cooking part of the game. Firstly, we need a timer on each pancake so we could record how long it's been cooking. Look in <Pancake /> - the startInterval() method is already there. All you need to do is add a lifecycle method in <Pancake /> at componentDidMount() (that's the point at which the pancake component gets added to the page) which will start the counter.

Tidy up the timer

Now that we've set up a timer, we need to make sure we also remove it as soon as it's not needed anymore. It is important to always clean up such long-running processes as soon as they are no longer needed. You wouldn't leave your dirty dishes on the table after you've finished eating - similarly you shouldn't be leaving your intervals ticking after the component using them has been dismounted. As you can see, a cleanUpInterval() function is all set up for you, so all you have left to do it call it just before the component gets unmounted from the page. That will be in the componentWillUnmount() method.

That's it! You've finished off the game! Happy pancake-making!

Resources

react-component-mounting-and-unmounting-lab's People

Contributors

achasveachas avatar aturkewi avatar dependabot[bot] avatar gj avatar humzahchoudry avatar ihollander avatar kadikraman avatar kjleitz avatar kwebster2 avatar lizbur10 avatar lukeghenco avatar maxwellbenton avatar mdo5004 avatar pletcher avatar thomastuts avatar

Watchers

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

react-component-mounting-and-unmounting-lab's Issues

mocha tests breaking

Running tests with Learn works, but running them with mocha will not pass the third test. The error seems to come from line 31 in index-test.js. The line reads wrapper.unmount();

The error message in mocha is:
Invariant Violation: dangerouslyReplaceNodeWithMarkup(...): Cannot render markup in a worker thread. Make sure window and document are available globally before requiring React when unit testing or use ReactDOMServer.renderToString() for server rendering.

Should use import

index.js should use "import Game from './components/Game'" instead of "const Game = require('./components/Game');"

npm start error

when I use npm start it loads the previous lab and not the new one. I'm not sure how to fix it. Maybe in package.json? The name has the previous lab's name.

Easy

This lab is remarkably easy. Not necessarily an issue, but I would have appreciated a bit more of a challenge to better learn this material. As is, you barely even code, took me about 30 seconds to complete, and it is the only lab for the entire section.

Bug in the code

The logic for getPancakeStatus introduces a bug. Because it's checking !flippedAt instead of checking explicitly for undefined (ie typeof this.state.flippedAt === 'undefined' ), it is possible to get a truthy value for !flippedAt if you flip at 0 seconds. Because 0 is a falsy value in JS, if you flip @ 0 seconds and then flip the other side at two seconds, you get a return value of 'cooked' from getPancakeStatus.

Also, setting something explicitly to undefined goes against previous guidance in the curriculum (top tip halfway down the page), so if it is ok to do so in this case, it might be a good idea to add an extra sentence for an explanation.

I would recommend checking for an explicit undefined value in getPancakeStatus instead of just a falsy value to remove the bug.

issue in submitting lab

Hmm...this doesn't appear to be a Learn lesson. Are you in the right directory?

I am getting this error while trying to submit this lab and some other labs.

Couldn't open app in browser. fix in comments

npm run bundle would work, but when I tried to view index.html in the browser, nothing would render. There were two errors in the console.

The problem was the way files were being 'required' in index.js instead of being 'imported'. To fix,
in Index.js, on lines 1, 2, and 4 change this:

const React = require('react');
const ReactDOM = require('react-dom');

const Game = require('./components/Game');

to this:

import React from 'react';
import ReactDOM from 'react-dom';

import Game from './components/Game';

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.