Git Product home page Git Product logo

component-lifecycle's Introduction

7.2.7 Activity : Component Lifecycle

Getting Started

  • Hit run
  • Explore the code you currently have
  • Open the devtools console in your browser (recommended to open the app in a new tab by clicking the button to the right of the Repl.it internal URL bar)

Mounting

  • Import the RocketLauncher component on App.jsx, then render it underneath <h1>Learning Cleanup!</h1>
  • At this point, you should notice that the console printed "Rocket launcher mounted" and started counting down from 300000.
  return (
    <main>
      <h1>Learning Cleanup!</h1>
      <RocketLauncher />
    </main>
  )

Unmounting

  • To trigger an unmount, conditionally render <RocketLauncher /> based on the state of launch.
  • We will also need a button that inverts the state of launch on a click event: onClick.
  return (
    <main>
      <h1>Learning Cleanup!</h1>
           <button onClick={() => setLaunch(!launch)}>
             { launch ? "Abort Rocket Launch" : "Start Rocket Launch" }
           </button>
      { launch ? <RocketLauncher/> : null }
    </main>
  )
  • Next, in the app browser, click the button a few times to toggle the rocket launcher on and off. We should see the "Rocket launcher mounted" console log when it enters the screen. We should also see multiple counters counting down at the same time.

  • It may now be obvious why we need a cleanup, which we will implement next.

Cleanup

  • Inside of the useEffect callback in RocketLauncher.jsx, return an anonymous function.
  • This function should first console.log('Rocket launcher unmounted'), then clearInterval(interval).
useEffect(() => {
    const countdown = () => console.log(counter -= 1)
    console.log('Rocket launcher mounted')
    let interval = setInterval(countdown, 1000)

    return () => {
      console.log('Rocket launcher unmounted')
      clearInterval(interval)
    }
  })
  • Once that is done, try toggling your timer again. Much better!

Let's see an example of an update

  • Add a state variable called rocketName to your app. Instantiate the variable with 'Apollo'.
  • Next, we will define a function that handles changing the rocketName state variable, so we can specify what rocket is launching.
  let [rocketName, setRocketName] = useState('Apollo')

  const handleChange = (e) => {
    setRocketName(e.target.value)
  }
  • To use this function, render a text input and have it call the function onChange.
  <input type="text" onChange={handleChange} />
  • To see the change happen, we have to use the rocketName variable somewhere. Replace the "Learning Cleanup!" text with the variable.
<h1>{rocketName}</h1>
  • Now try typing in your input while the rocket launcher is on. Watch what happens in the console. Because the App-level is re-rendering every time the state variable rocketName changes, we are completely resetting the countdown, updating the component, each time we press a key.

Taking it further...

  • Currently, the rocketName change causes the countdown to completely reset. Try to come up with a way to prevent this behavior from happening! hint: you will need to move existing elements into a new component!
  • Consider how one could dynamically render the value of counter to the screen. What changes would we need to make in order to cause that render?

component-lifecycle's People

Contributors

legomycode avatar

Watchers

 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.