Skip to content

Commit

Permalink
improve things
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed May 17, 2024
1 parent 4429196 commit d788a9f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
8 changes: 5 additions & 3 deletions exercises/06.rerenders/02.problem.comparator/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const Avatar = memo(
return <img src={user.avatarUrl} alt={user.name} />
},
(prevProps, nextProps) => {
const avatarChanged = prevProps.user.avatarUrl !== nextProps.user.avatarUrl
const nameChanged = prevProps.user.name !== nextProps.user.name
return avatarChanged || nameChanged
const avatarUnchanged =
prevProps.user.avatarUrl === nextProps.user.avatarUrl
const nameUnchanged = prevProps.user.name === nextProps.user.name
// return true if we don't want to re-render
return avatarUnchanged || nameUnchanged
},
)
```
Expand Down
2 changes: 1 addition & 1 deletion exercises/06.rerenders/02.problem.comparator/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const ListItem = memo(
)
},
// 🐨 add a custom comparator function here make sure to handle the following cases:
// 1. if the index or item change
// 1. if the props "index", "city", or "getItemProps" change
// 2. if the item's "selected" state has changed
// 3. if the item's "highlighted" state has changed
)
1 change: 1 addition & 0 deletions exercises/06.rerenders/02.solution.comparator/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const ListItem = memo(
return (
prevProps.index === nextProps.index &&
prevProps.city === nextProps.city &&
prevProps.getItemProps === nextProps.getItemProps &&
prevIsSelected === nextIsSelected &&
prevIsHighlighted === nextIsHighlighted
)
Expand Down
8 changes: 5 additions & 3 deletions exercises/06.rerenders/03.problem.primitives/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const Avatar = memo(
return <img src={user.avatarUrl} alt={user.name} />
},
(prevProps, nextProps) => {
const avatarChanged = prevProps.user.avatarUrl !== nextProps.user.avatarUrl
const nameChanged = prevProps.user.name !== nextProps.user.name
return avatarChanged || nameChanged
const avatarUnchanged =
prevProps.user.avatarUrl === nextProps.user.avatarUrl
const nameUnchanged = prevProps.user.name === nextProps.user.name
// return true if we don't want to re-render
return avatarUnchanged || nameUnchanged
},
)
```
Expand Down
6 changes: 1 addition & 5 deletions exercises/06.rerenders/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

Here's the lifecycle of a React app:

```
→ render → reconciliation → commit
↖ ↙
state change
```
![→ render → reconciliation → commit → state change → rerender](/img/react-render-cycle.png)

Let's define a few terms:

Expand Down
Binary file added public/img/react-render-cycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d788a9f

Please sign in to comment.