Responding to Events

Can event handlers have side effects?

Absolutely! Event handlers are the best place for side effects.

Unlike rendering functions, event handlers don’t need to be pure, so it’s a great place to change something—for example, change an input’s value in response to typing, or change a list in response to a button press. However, in order to change some information, you first need some way to store it. In React, this is done by using state, a component’s memory. You will learn all about it on the next page.

⇒ 요거 기억하자. 헷갈릴 수 있는 개념인듯.

State: A Component’s Memory

Queueing a Series of State Updates

  queue.forEach(v => {
    if(typeof v === 'function'){
      finalState = v(finalState);
    }else{
      finalState = v
    }
  })
  for (let update of queue) {
    if (typeof update === 'function') {
      // Apply the updater function.
      finalState = update(finalState);
    } else {
      // Replace the next state.
      finalState = update;
    }
  }

Updating Objects in State