Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 65 additions & 18 deletions src/content/learn/manipulating-the-dom-with-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ export default function CatFriends() {
<nav>
<button onClick={() => scrollToCat(catList[0])}>Neo</button>
<button onClick={() => scrollToCat(catList[5])}>Millie</button>
<button onClick={() => scrollToCat(catList[9])}>Bella</button>
<button onClick={() => scrollToCat(catList[8])}>Bella</button>
</nav>
<div>
<ul>
{catList.map((cat) => (
<li
key={cat}
key={cat.id}
ref={(node) => {
const map = getMap();
map.set(cat, node);
Expand All @@ -263,7 +263,7 @@ export default function CatFriends() {
};
}}
>
<img src={cat} />
<img src={cat.imageUrl} />
</li>
))}
</ul>
Expand All @@ -273,11 +273,22 @@ export default function CatFriends() {
}

function setupCatList() {
const catList = [];
for (let i = 0; i < 10; i++) {
catList.push("https://loremflickr.com/320/240/cat?lock=" + i);
const catCount = 10;
const catList = new Array(catCount)
for (let i = 0; i < catCount; i++) {
let imageUrl = '';
if (i < 5) {
imageUrl = "https://placecats.com/neo/320/240";
} else if (i < 8) {
imageUrl = "https://placecats.com/millie/320/240";
} else {
imageUrl = "https://placecats.com/bella/320/240";
}
catList[i] = {
id: i,
imageUrl,
};
}

return catList;
}

Expand Down Expand Up @@ -876,12 +887,30 @@ export default function CatFriends() {
);
}

const catList = [];
for (let i = 0; i < 10; i++) {
catList.push({
const catCount = 10;
const catList = new Array(catCount);
for (let i = 0; i < catCount; i++) {
const bucket = Math.floor(Math.random() * catCount) % 2;
let imageUrl = '';
switch (bucket) {
case 0: {
imageUrl = "https://placecats.com/neo/250/200";
break;
}
case 1: {
imageUrl = "https://placecats.com/millie/250/200";
break;
}
case 2:
default: {
imageUrl = "https://placecats.com/bella/250/200";
break;
}
}
catList[i] = {
id: i,
imageUrl: 'https://loremflickr.com/250/200/cat?lock=' + i
});
imageUrl,
};
}

```
Expand Down Expand Up @@ -961,7 +990,7 @@ export default function CatFriends() {
behavior: 'smooth',
block: 'nearest',
inline: 'center'
});
});
}}>
Next
</button>
Expand Down Expand Up @@ -993,12 +1022,30 @@ export default function CatFriends() {
);
}

const catList = [];
for (let i = 0; i < 10; i++) {
catList.push({
const catCount = 10;
const catList = new Array(catCount);
for (let i = 0; i < catCount; i++) {
const bucket = Math.floor(Math.random() * catCount) % 2;
let imageUrl = '';
switch (bucket) {
case 0: {
imageUrl = "https://placecats.com/neo/250/200";
break;
}
case 1: {
imageUrl = "https://placecats.com/millie/250/200";
break;
}
case 2:
default: {
imageUrl = "https://placecats.com/bella/250/200";
break;
}
}
catList[i] = {
id: i,
imageUrl: 'https://loremflickr.com/250/200/cat?lock=' + i
});
imageUrl,
};
}

```
Expand Down
Loading