Skip to content

Commit a467b67

Browse files
Fix: Add missing Chat.js and complete ContactList.js dispatch logic in useReducer Messenger example
1 parent e07ac94 commit a467b67

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/content/learn/extracting-state-logic-into-a-reducer.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,16 +1189,19 @@ export function messengerReducer(state, action) {
11891189
```
11901190

11911191
```js src/ContactList.js
1192-
export default function ContactList({contacts, selectedId, dispatch}) {
1192+
export default function ContactList({ contacts, selectedId, dispatch }) {
11931193
return (
11941194
<section className="contact-list">
11951195
<ul>
11961196
{contacts.map((contact) => (
11971197
<li key={contact.id}>
11981198
<button
1199-
onClick={() => {
1200-
// TODO: dispatch changed_selection
1201-
}}>
1199+
onClick={() =>
1200+
dispatch({
1201+
type: 'changed_selection',
1202+
contactId: contact.id
1203+
})
1204+
}>
12021205
{selectedId === contact.id ? <b>{contact.name}</b> : contact.name}
12031206
</button>
12041207
</li>
@@ -1207,27 +1210,29 @@ export default function ContactList({contacts, selectedId, dispatch}) {
12071210
</section>
12081211
);
12091212
}
1213+
12101214
```
12111215

12121216
```js src/Chat.js
1213-
import { useState } from 'react';
12141217

1215-
export default function Chat({contact, message, dispatch}) {
1218+
export default function Chat({ contact, message, dispatch }) {
12161219
return (
12171220
<section className="chat">
1221+
<h2>Chat with {contact.name}</h2>
12181222
<textarea
12191223
value={message}
1220-
placeholder={'Chat to ' + contact.name}
1221-
onChange={(e) => {
1222-
// TODO: dispatch edited_message
1223-
// (Read the input value from e.target.value)
1224-
}}
1224+
placeholder={`Message to ${contact.name}`}
1225+
onChange={(e) =>
1226+
dispatch({
1227+
type: 'edited_message',
1228+
message: e.target.value
1229+
})
1230+
}
12251231
/>
1226-
<br />
1227-
<button>Send to {contact.email}</button>
12281232
</section>
12291233
);
12301234
}
1235+
12311236
```
12321237

12331238
```css

0 commit comments

Comments
 (0)