Bug: duplicate onClick handlers cause 404 on toggleImportance in Part 6 TanStack Query example#4298
Open
Matt-Anis wants to merge 1 commit into
Open
Bug: duplicate onClick handlers cause 404 on toggleImportance in Part 6 TanStack Query example#4298Matt-Anis wants to merge 1 commit into
Matt-Anis wants to merge 1 commit into
Conversation
**Part:** 6 — TanStack Query
**Description:**
In the TanStack Query example, the `toggleImportance` function is triggered twice when clicking the button, due to both the `<li>` and the nested `<button>` having `onClick` handlers. Because of event bubbling, clicking the button fires both handlers.
The two handlers also pass different arguments:
- `<li onClick={() => toggleImportance(note)}>` — passes the full note object ✓
- `<button onClick={() => toggleImportance(note.id)}>` — passes only the id string ✗
Since `toggleImportance` expects a full note object and spreads it (`{ ...note, important: !note.important }`), passing just the id string results in `PUT /notes/undefined` (404) from the button handler, followed by a correct `PUT /notes/:id` (200) from the `li` handler.
**Reproduction:**
Open the browser Network tab and click any "make important" button — two PUT requests fire, the first returning 404 and the second 200.
**Suggested fix:**
Remove the `onClick` from the `<li>` and fix the `<button>` to pass the full note object:
```jsx
<li key={note.id}>
{note.important ? <strong>{note.content}</strong> : note.content}
<button onClick={() => toggleImportance(note)}>
{note.important ? 'make not important' : 'make important'}
</button>
</li>
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part: 6 — TanStack Query
Description:
In the TanStack Query example, the
toggleImportancefunction is triggered twice when clicking the button, due to both the<li>and the nested<button>havingonClickhandlers. Because of event bubbling, clicking the button fires both handlers.The two handlers also pass different arguments:
<li onClick={() => toggleImportance(note)}>— passes the full note object ✓<button onClick={() => toggleImportance(note.id)}>— passes only the id string ✗Since
toggleImportanceexpects a full note object and spreads it ({ ...note, important: !note.important }), passing just the id string results inPUT /notes/undefined(404) from the button handler, followed by a correctPUT /notes/:id(200) from thelihandler.Reproduction:
Open the browser Network tab and click any "make important" button — two PUT requests fire, the first returning 404 and the second 200.
Suggested fix:
Remove the
onClickfrom the<li>and fix the<button>to pass the full note object: