Choosing the State Structure·续3
```js src/AddItem.js hidden import { useState } from 'react';
export default function AddItem({ onAddItem }) { const [title, setTitle] = useState(''); return ( <> setTitle(e.target.value)} /> </> ) }
```js src/PackingList.js hidden
import { useState } from 'react';
export default function PackingList({
items,
onChangeItem,
onDeleteItem
}) {
return (
<ul>
{items.map(item => (
<li key={item.id}>
<label>
<input
type="checkbox"
checked={item.packed}
onChange={e => {
onChangeItem({
...item,
packed: e.target.checked
});
}}
/>
{' '}
{item.title}
</label>
<button onClick={() => onDeleteItem(item.id)}>
Delete
</button>
</li>
))}
</ul>
);
}
button { margin: 5px; }
li { list-style-type: none; }
ul, li { margin: 0; padding: 0; }
Notice how the event handlers are only concerned with calling setItems after this change. The item counts are now calculated during the next render from items, so they are always up-to-date.
Fix the disappearing selection
There is a list of letters in state. When you hover or focus a particular letter, it gets highlighted. The currently highlighted letter is stored in the highlightedLetter state variable. You can "star" and "unstar" individual letters, which updates the letters array in state.
This code works, but there is a minor UI glitch. When you press "Star" or "Unstar", the highlighting disappears for a moment. However, it reappears as soon as you move your pointer or switch to another letter with keyboard. Why is this happening? Fix it so that the highlighting doesn't disappear after the button click.
```js src/App.js import { useState } from 'react'; import { initialLetters } from './data.js'; import Letter from './Letter.js';
export default function MailClient() { const [letters, setLetters] = useState(initialLetters); const [highlightedLetter, setHighlightedLetter] = useState(null);
function handleHover(letter) { setHighlightedLetter(letter); }
function handleStar(starred) { setLetters(letters.map(letter => { if (letter.id === starred.id) { return { ...letter, isStarred: !letter.isStarred }; } else { return letter; } })); }
return ( <>
Inbox
-
{letters.map(letter => (
```js src/Letter.js
export default function Letter({
letter,
isHighlighted,
onHover,
onToggleStar,
}) {
return (
<li
className={
isHighlighted ? 'highlighted' : ''
}
onFocus={() => {
onHover(letter);
}}
onPointerMove={() => {
onHover(letter);
}}
>
<button onClick={() => {
onToggleStar(letter);
}}>
{letter.isStarred ? 'Unstar' : 'Star'}
</button>
{letter.subject}
</li>
)
}
```js src/data.js export const initialLetters = [{ id: 0, subject: 'Ready for adventure?', isStarred: true, }, { id: 1, subject: 'Time to check in!', isStarred: false, }, { id: 2, subject: 'Festival Begins in Just SEVEN Days!', isStarred: false, }];
```css
button { margin: 5px; }
li { border-radius: 5px; }
.highlighted { background: #d2eaff; }
The problem is that you're holding the letter object in highlightedLetter. But you're also holding the same information in the letters array. So your state has duplication! When you update the letters array after the button click, you create a new letter object which is different from highlightedLetter. This is why highlightedLetter === letter check becomes false, and the highlight disappears. It reappears the next time you call setHighlightedLetter when the pointer moves.
To fix the issue, remove the duplication from state. Instead of storing the letter itself in two places, store the highlightedId instead. Then you can check isHighlighted for each letter with letter.id === highlightedId, which will work even if the letter object has changed since the last render.
```js src/App.js import { useState } from 'react'; import { initialLetters } from './data.js'; import Letter from './Letter.js';
export default function MailClient() { const [letters, setLetters] = useState(initialLetters); const [highlightedId, setHighlightedId ] = useState(null);
function handleHover(letterId) { setHighlightedId(letterId); }
function handleStar(starredId) { setLetters(letters.map(letter => { if (letter.id === starredId) { return { ...letter, isStarred: !letter.isStarred }; } else { return letter; } })); }
return ( <>
Inbox
-
{letters.map(letter => (
```js src/Letter.js
export default function Letter({
letter,
isHighlighted,
onHover,
onToggleStar,
}) {
return (
<li
className={
isHighlighted ? 'highlighted' : ''
}
onFocus={() => {
onHover(letter.id);
}}
onPointerMove={() => {
onHover(letter.id);
}}
>
<button onClick={() => {
onToggleStar(letter.id);
}}>
{letter.isStarred ? 'Unstar' : 'Star'}
</button>
{letter.subject}
</li>
)
}
```js src/data.js export const initialLetters = [{ id: 0, subject: 'Ready for adventure?', isStarred: true, }, { id: 1, subject: 'Time to check in!', isStarred: false, }, { id: 2, subject: 'Festival Begins in Just SEVEN Days!', isStarred: false, }];
```css
button { margin: 5px; }
li { border-radius: 5px; }
.highlighted { background: #d2eaff; }
Implement multiple selection
In this example, each Letter has an isSelected prop and an onToggle handler that marks it as selected. This works, but the state is stored as a selectedId (either null or an ID), so only one letter can get selected at any given time.
Change the state structure to support multiple selection. (How would you structure it? Think about this before writing the code.) Each checkbox should become independent from the others. Clicking a selected letter should uncheck it. Finally, the footer should show the correct number of the selected items.
Instead of a single selected ID, you might want to hold an array or a Set of selected IDs in state.
```js src/App.js import { useState } from 'react'; import { letters } from './data.js'; import Letter from './Letter.js';
export default function MailClient() { const [selectedId, setSelectedId] = useState(null);
// TODO: allow multiple selection const selectedCount = 1;
function handleToggle(toggledId) { // TODO: allow multiple selection setSelectedId(toggledId); }
return ( <>
Inbox
-
{letters.map(letter => (
You selected {selectedCount} letters
```js src/Letter.js
export default function Letter({
letter,
onToggle,
isSelected,
}) {
return (
<li className={
isSelected ? 'selected' : ''
}>
<label>
<input
type="checkbox"
checked={isSelected}
onChange={() => {
onToggle(letter.id);
}}
/>
{letter.subject}
</label>
</li>
)
}
```js src/data.js export const letters = [{ id: 0, subject: 'Ready for adventure?', isStarred: true, }, { id: 1, subject: 'Time to check in!', isStarred: false, }, { id: 2, subject: 'Festival Begins in Just SEVEN Days!', isStarred: false, }];
```css
input { margin: 5px; }
li { border-radius: 5px; }
label { width: 100%; padding: 5px; display: inline-block; }
.selected { background: #d2eaff; }
Instead of a single selectedId, keep a selectedIds array in state. For example, if you select the first and the last letter, it would contain [0, 2]. When nothing is selected, it would be an empty [] array:
```js src/App.js import { useState } from 'react'; import { letters } from './data.js'; import Letter from './Letter.js';
export default function MailClient() { const [selectedIds, setSelectedIds] = useState([]);
const selectedCount = selectedIds.length;
function handleToggle(toggledId) { // Was it previously selected? if (selectedIds.includes(toggledId)) { // Then remove this ID from the array. setSelectedIds(selectedIds.filter(id => id !== toggledId )); } else { // Otherwise, add this ID to the array. setSelectedIds([ ...selectedIds, toggledId ]); } }
return ( <>
Inbox
-
{letters.map(letter => (
You selected {selectedCount} letters
```js src/Letter.js
export default function Letter({
letter,
onToggle,
isSelected,
}) {
return (
<li className={
isSelected ? 'selected' : ''
}>
<label>
<input
type="checkbox"
checked={isSelected}
onChange={() => {
onToggle(letter.id);
}}
/>
{letter.subject}
</label>
</li>
)
}
```js src/data.js export const letters = [{ id: 0, subject: 'Ready for adventure?', isStarred: true, }, { id: 1, subject: 'Time to check in!', isStarred: false, }, { id: 2, subject: 'Festival Begins in Just SEVEN Days!', isStarred: false, }];
```css
input { margin: 5px; }
li { border-radius: 5px; }
label { width: 100%; padding: 5px; display: inline-block; }
.selected { background: #d2eaff; }
One minor downside of using an array is that for each item, you're calling selectedIds.includes(letter.id) to check whether it's selected. If the array is very large, this can become a performance problem because array search with includes() takes linear time, and you're doing this search for each individual item.
To fix this, you can hold a Set in state instead, which provides a fast has() operation:
```js src/App.js import { useState } from 'react'; import { letters } from './data.js'; import Letter from './Letter.js';
export default function MailClient() { const [selectedIds, setSelectedIds] = useState( new Set() );
const selectedCount = selectedIds.size;
function handleToggle(toggledId) { // Create a copy (to avoid mutation). const nextIds = new Set(selectedIds); if (nextIds.has(toggledId)) { nextIds.delete(toggledId); } else { nextIds.add(toggledId); } setSelectedIds(nextIds); }
return ( <>
Inbox
-
{letters.map(letter => (
You selected {selectedCount} letters
```js src/Letter.js
export default function Letter({
letter,
onToggle,
isSelected,
}) {
return (
<li className={
isSelected ? 'selected' : ''
}>
<label>
<input
type="checkbox"
checked={isSelected}
onChange={() => {
onToggle(letter.id);
}}
/>
{letter.subject}
</label>
</li>
)
}
```js src/data.js export const letters = [{ id: 0, subject: 'Ready for adventure?', isStarred: true, }, { id: 1, subject: 'Time to check in!', isStarred: false, }, { id: 2, subject: 'Festival Begins in Just SEVEN Days!', isStarred: false, }];
```css
input { margin: 5px; }
li { border-radius: 5px; }
label { width: 100%; padding: 5px; display: inline-block; }
.selected { background: #d2eaff; }
Now each item does a selectedIds.has(letter.id) check, which is very fast.
Keep in mind that you should not mutate objects in state, and that includes Sets, too. This is why the handleToggle function creates a copy of the Set first, and then updates that copy.
