Learning object 46 of 100
State-to-interface rendering explorer
Edit a state object and see a render function rebuild the interface from it, safely, every time.
Loading…
What it shows
A render function reads state and makes the page match it. Calling it after every change keeps the page correct without patching individual elements. replaceChildren avoids duplicates, and building elements with textContent keeps user data from being interpreted as HTML.
Guided experiment
- Change the state in the code (add a task, mark one done) and run again.
- In Unsafe vs safe, compare how the two lists render the same names.
- Call render three times in a row. Does anything duplicate?
Predict
render() uses replaceChildren and is called three times with the same two items. How many li are there?
Related
Lessons: Lesson 67: Rendering interfaces from state
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-046.js in the companion repository.