JavaScript AcademyProfessional Engineering

Learning object 53 of 100

Component lifecycle model

Create, update and destroy component instances and watch props flow in, callbacks flow out and listeners being removed on teardown.

Loading…

What it shows

A reusable component is a function that receives data and options (props) and returns an element. It reports events through callbacks instead of changing the page itself, keeps its own state in local variables, and can offer a destroy function that removes its listeners, for example with an AbortController. Each instance is independent.

Guided experiment

  1. Click Create twice, then use each counter. Are they independent?
  2. Click Destroy on one counter. Do its buttons still log?
  3. Change the step prop in the code and run again.

Predict

Two counters are created from the same component function. The first is clicked twice. What does the second show?

Related

Lessons: Lesson 72: Reusable UI components, Lesson 60: Building interactive components

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-053.js in the companion repository.