GithubHelp home page GithubHelp logo

react-rendering-lab-v-000's Introduction

React Rendering

Objectives

  1. Explain what happens when React renders a component
  2. Explain why render() must be idempotent
  3. Describe the parts of the component lifecycle leading up to and coming after render()
  4. Practice using different parts of the render/update portion of the React component lifecycle.

Overview

In this module, we're going to have a go at actually using each of the component lifecycle methods.

componentWillReceiveProps()

Let's have a look at the first section. This is a little online survey of a book. People can rate the book based on how much they enjoyed it. They have 3 options: 'loved it', 'hated it' and 'indifferent'. We want to record not only the score the audience gave to the book, but also the trend of ratings as they are being given. So for example, if someone likes the book, the average score is increasing, if they dislike the book, it would be decreasing and if they're indifferent the score is stable. Have a look at <Rating /> and <Survey /> and implement a componentWillReceiveProps() which will compare the nextProps.rating (passed into the method) to this.props.rating and calls this.setState with the correct values.

By the end of this section, all the tests under <Rating /> section should be passing.

shouldComponentUpdate()

For this section, run the sample app and open the developer console. Look at the second section of tasks. You should see 3 circles: red, yellow and green. There is also a button which swaps the color of red and green circles. If you look at the developer console, we are logging out a message whenever a circle is re-rendered. Notice that all 3 circles get re-rendered whenever we swap the colors.

The easiest way to prevent this unnecessary rerender is by using the shouldComponentUpdate() lifecycle hook. Open Circle.js and add a shouldComponentUpdate() which will return true only if the new color being passed in is different from the existing one.

By the end of this section, the test under <Circle /> should be passing.

componentWillUpdate()

For this section, we've got a little random .gif generator. Pressing the 'New random .gif!' button will make/perform a request to giphy and display a new random .gif for us! Neat! Try pressing the button now. You might have noticed that it takes a bit of time for the new .gif to display. Perhaps we'd like a little indication of the loading. Maybe a loading bar? Now, in reality you could do this loading bar in React, however for the sake of this example, lets imagine that the loading bar is done by a different library that React has no control over. You can set this loading bar in the showLoadingBar() function which is already created for you in <Animation />. Call this function through the componentWillUpdate() lifecycle method and see that happens! If you've done it correctly you should see a pink loading bar at the top of the screen every time a new .gif is being loaded.

By the end of this section, the test under <Animation /> should be passing.

componentDidUpdate()

componentDidUpdate() is generally used to interact with elements outside of React. Suppose we had a Pikachu library that handles rendering a Pikachu on the page. The Pikachu image is not part of the React application, but we need to be able to interact with it within React. Open the <Pikachu /> component and observe it renders two buttons: a button for making the Pikachu bigger and another one for making it smaller. You can also see that pressing the buttons will change the component's internal state to reflect how big the Pikachu should be. There is already a method that resizes the Pikachu based on new inputs. All we need to do is add a componentDidUpdate() to call it! Go on, have a go!

By the end of this section, the test under <Pikachu /> should be passing.

Resources

View Rendering Lab on Learn.co and start learning to code for free.

react-rendering-lab-v-000's People

Contributors

dakotalmartinez avatar danielseehausen avatar kjleitz avatar lukeghenco avatar mlawford avatar pickledyamsman avatar pletcher avatar realandrewcohn avatar thomastuts avatar

Stargazers

 avatar

Watchers

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

react-rendering-lab-v-000's Issues

All Tests pass, won't open in browser

Even with all tests passing, running npm start only results in an error not being handled and the server crashing. According to the ReadMe... this lab should run before you edit it.

Error during test

Received an error during test not allowing me to run any of them.

/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:328
        throw e;
        ^

Error: Couldn't find preset "es2015" relative to directory "/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000"
    at /Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at compile (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-register/lib/node.js:103:45)
    at loader (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-register/lib/node.js:144:14)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/mocha/lib/mocha.js:514:10)
    at Object.<anonymous> (/Users/admin/Dev/code/labs/react/react-rendering-lab-v-000/node_modules/mocha/bin/_mocha:480:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

labs/react/react-rendering-lab-v-000  master ✗                       2d ⚑
▶ /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/strategies/mocha.rb:42:in `results': undefined method `[]' for nil:NilClass (NoMethodError)
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/reporter.rb:47:in `report'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/reporter.rb:13:in `report'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/runner.rb:44:in `report_and_clean'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/runner.rb:21:in `block in run'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/runner.rb:20:in `fork'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/lib/learn_test/runner.rb:20:in `run'
	from /Users/admin/.rvm/gems/ruby-2.3.3/gems/learn-test-2.5.1/bin/learn-test:64:in `<top (required)>'
	from /Users/admin/.rvm/gems/ruby-2.3.3/bin/learn-test:22:in `load'
	from /Users/admin/.rvm/gems/ruby-2.3.3/bin/learn-test:22:in `<main>'
	from /Users/admin/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `eval'
	from /Users/admin/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `<main>'

I followed the resolution from a previous issues in order to run and pass this lab.
Resolution: #176

Typo in Test for Animation component

The test says componentWillUpate. The word Update is misspelled. I had a hard time passing the lab until I noticed the typo in the test. I'm able to pass now.

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.