Lesson 1 of 8

State in one place

Goal

Change the state and the UI follows

01

Explanation

The surest way to wreck a UI is to change the DOM from ten different places: you add an element here and remove some text there, and soon nobody knows what the page's real state is.

The alternative is a single rule: one object holds the state, and one function draws it on the page. Every event changes the state and then calls render(), and it never touches the page itself.

This is the idea React and libraries like it are built on, but it doesn't take a library, just the discipline to stick to the rule.

02

Your task

Write the render() function so it draws state.items, and make the button add an item to the state and then re-render.

Checks

5 checks
  1. The list shows the two items in the state
  2. The counter matches the number of items in the state
  3. Adding grows the list and the counter together
  4. The state itself grows, not just the DOM
  5. The render function is defined and can be called on its own
03

Hints

0/3
Track lessons0 of 8 done

A UI is built from data, so here you learn to filter, sort and group that data without touching the DOM directly.

Intermediate level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

A UI is built from data, so here you learn to filter, sort and group that data without touching the DOM directly.

Intermediate level, 8 lessons, 2 hours in total.