/README.md

https://gitlab.com/learn-co-curriculum/react-rendering-lab · Markdown · 68 lines · 49 code · 19 blank · 0 comment · 0 complexity · c5d4e8405969b97119be03fb04bc4536 MD5 · raw file

  1. # React Rendering
  2. ## Objectives
  3. 1. Explain what happens when React renders a component
  4. 2. Explain why `render()` must be idempotent
  5. 3. Describe the parts of the component lifecycle leading up to and coming after
  6. `render()`
  7. 4. Practice using different parts of the render/update portion of the React
  8. component lifecycle.
  9. ## Overview
  10. In this module, we're going to have a go at actually using each of the component lifecycle methods.
  11. ### `componentWillReceiveProps()`
  12. Let's have a look at the first section. This is a little online survey of a book. People can rate the
  13. book based on how much they enjoyed it. They have 3 options: 'loved it', 'hated it' and 'indifferent'.
  14. We want to record not only the score the audience gave to the book, but also the trend of ratings as
  15. they are being given. So for example, if someone likes the book, the average score is *increasing*, if they
  16. dislike the book, it would be *decreasing* and if they're indifferent the score is *stable*. Have a look at
  17. `<Rating />` and `<Survey />` and implement a `componentWillReceiveProps()` which will compare the `nextProps.rating`
  18. (passed into the method) to `this.props.rating` and calls `this.setState` with the correct values.
  19. By the end of this section, all the tests under `<Rating />` section should be passing.
  20. ### `shouldComponentUpdate()`
  21. For this section, run the sample app and open the developer console. Look at the second section of tasks. You should
  22. see 3 circles: red, yellow and green. There is also a button which swaps the color of red and green circles. If you look
  23. at the developer console, we are logging out a message whenever a circle is re-rendered. Notice that all 3 circles get
  24. re-rendered whenever we swap the colors.
  25. The easiest way to prevent this unnecessary rerender is by using the `shouldComponentUpdate()` lifecycle hook. Open
  26. `Circle.js` and add a `shouldComponentUpdate()` which will return `true` only if the new color being passed in is different
  27. from the existing one.
  28. By the end of this section, the test under `<Circle />` should be passing.
  29. ### `componentWillUpdate()`
  30. For this section, we've got a little random .gif generator. Pressing the 'New random .gif!' button will make/perform a request
  31. to giphy and display a new random .gif for us! Neat! Try pressing the button now. You might have noticed
  32. 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
  33. loading bar? Now, in reality you could do this loading bar in React, however for the sake of this example, lets imagine
  34. that the loading bar is done by a different library that React has no control over. You can set this loading bar in the
  35. `showLoadingBar()` function which is already created for you in `<Animation />`. Call this function through the
  36. `componentWillUpdate()` lifecycle method and see that happens! If you've done it correctly you should see a pink loading
  37. bar at the top of the screen every time a new .gif is being loaded.
  38. By the end of this section, the test under `<Animation />` should be passing.
  39. ### `componentDidUpdate()`
  40. `componentDidUpdate()` is generally used to interact with elements outside of React. Suppose we had a Pikachu library that
  41. handles rendering a Pikachu on the page. The Pikachu image is not part of the React application, but we need to be able to
  42. interact with it within React. Open the `<Pikachu />` component and observe it renders two buttons: a button for making the
  43. Pikachu bigger and another one for making it smaller. You can also see that pressing the buttons will change the component's
  44. internal state to reflect how big the Pikachu should be. There is already a method that resizes the Pikachu based on new
  45. inputs. All we need to do is add a `componentDidUpdate()` to call it! Go on, have a go!
  46. By the end of this section, the test under `<Pikachu />` should be passing.
  47. ## Resources
  48. - [React: Component Specs and Lifecycle](https://github.com/learn-co-curriculum/react-rendering)
  49. <p class='util--hide'>View <a href='https://learn.co/lessons/react-rendering-lab'>Rendering Lab</a> on Learn.co and start learning to code for free.</p>