Skip to content

Bug: duplicate onClick handlers cause 404 on toggleImportance in Part 6 TanStack Query example#4298

Open
Matt-Anis wants to merge 1 commit into
fullstack-hy2020:sourcefrom
Matt-Anis:patch-1
Open

Bug: duplicate onClick handlers cause 404 on toggleImportance in Part 6 TanStack Query example#4298
Matt-Anis wants to merge 1 commit into
fullstack-hy2020:sourcefrom
Matt-Anis:patch-1

Conversation

@Matt-Anis

Copy link
Copy Markdown

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:

<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>

**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>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant