Extracting State Logic into a Reducer·续3

```js src/messengerReducer.js export const initialState = { selectedId: 0, message: 'Hello', };

export function messengerReducer(state, action) { switch (action.type) { case 'changed_selection': { return { ...state, selectedId: action.contactId, message: '', }; } case 'edited_message': { return { ...state, message: action.message, }; } case 'sent_message': { return { ...state, message: '', }; } default: { throw Error('Unknown action: ' + action.type); } } }


```js src/ContactList.js
export default function ContactList({contacts, selectedId, dispatch}) {
  return (
    <section className="contact-list">
      <ul>
        {contacts.map((contact) => (
          <li key={contact.id}>
            <button
              onClick={() => {
                dispatch({
                  type: 'changed_selection',
                  contactId: contact.id,
                });
              }}>
              {selectedId === contact.id ? <b>{contact.name}</b> : contact.name}
            </button>
          </li>
        ))}
      </ul>
    </section>
  );
}

```js src/Chat.js import { useState } from 'react';

export default function Chat({contact, message, dispatch}) { return (