Many loopsLesson 20 of 23

How does a child talk back?

Let a child report to whoever renders it without knowing who that is.

App keeps a list of counters. Each counter draws its own Remove button, because that is where the button belongs on screen. The list is not the counter's, though.

Predict

Read the ClickedRemove handler in Counter.ts, then the Model in App.ts. Press Remove in your head. What happens? And what would the counter need in order to do better?

Check your answer

Nothing happens on screen. The click reaches the counter's update, which returns its Model unchanged.

It has no other move. ids lives in App's Model, and a child can't reach its parent's Model any more than the parent can reach the child's. All the counter can do is say what happened, to whoever is drawing it.

Try it

  1. Press Remove and read the log. Which instance handled the click?

  2. Give the counter something to say. In Counter.ts, add the Schema below, list it on the component as events: CounterEvents, and change the handler to ($) => $.emit(CounterEvents.RequestedRemove()). Before you run and press Remove: is anyone listening?

export const CounterEvents = UiSchema.TaggedEnum({
  RequestedRemove: {}
})
  1. Now listen. In App.ts, add RequestedRemoveCounter: { id: Schema.Number } to AppMessage, and handle it with $.evolve({ ids: (ids) => ids.filter((other) => other !== id) }). Which component decides what a removal means?

  2. Connect the two where the child is declared, then press Remove and read the rows in order.

Counter(h, {
  key: String(id),
  props: { label: `Counter ${id}` },
  onEmit: {
    RequestedRemove: () => AppMessage.RequestedRemoveCounter({ id })
  }
})
  1. The counter never imports App. What else could a different parent do with the same event?

About step 5

Anything it likes: ask for confirmation first, archive instead of delete, or ignore it. The child asked. It didn't decide.

That is also why the event is named RequestedRemove and not Removed. Nothing has been removed when it is emitted.

What you found

A message stays inside its component. An event is what a component tells the outside, declared as a Schema like everything else. The parent maps each event to one of its own messages in onEmit, and from there it is an ordinary message through an ordinary update.

The log shows the whole exchange: the child's ClickedRemove with emit RequestedRemove, then the parent's RequestedRemoveCounter, then the counter leaving. It is lesson 11 one level up. The child reports and the parent decides, because each value has one owner. The count is the counter's. The list is App's.

Counter earns its loop because it has a count to keep. Not every piece of a screen does. Does everything need to be a component?

20
Principle 20

Props go down, events come up

A parent gives a child props. A child tells its parent what happened with events. Every value has exactly one owner.

ResultStarting

No messages yet

Use the example above. Each message that reaches update shows here, with what it changed.