You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`editClick`|`User`| Fired when edit button is clicked |
294
+
|`delete`|`User`| Fired after successful user deletion |
289
295
290
296
### NUsersUserCard
291
297
292
298
Displays individual user information with edit and delete actions. Used internally by `NUsersList` but can be used standalone.
293
299
The `edit` and `delete` events are emitted when the edit or delete button is clicked. The `delete` event is calling the API to delete the user, while the `edit` event is just emitting the user object and let the parent component handle the edit action.
294
300
301
+
### Automatic UI Updates
302
+
303
+
The `NUsersList` component follows an event-driven pattern for handling user updates. When a user is edited, the component emits events that the parent can handle to update the local state.
304
+
305
+
**Event Flow:**
306
+
1. User clicks edit → `NUsersUserCard` emits `editClick`
307
+
2.`NUsersList` forwards the event to parent → Parent receives `editClick`
308
+
3. Parent shows edit form → User submits changes
309
+
4. Parent updates local state → UI automatically reflects changes
310
+
311
+
**Example with event-driven updates:**
312
+
```vue
313
+
<script setup>
314
+
import { ref } from 'vue'
315
+
import { useUsers } from 'nuxt-users/composables'
316
+
317
+
const { users, updateUser } = useUsers()
318
+
const editingUser = ref(null)
319
+
320
+
const handleEdit = (user) => {
321
+
editingUser.value = user
322
+
}
323
+
324
+
const handleUserUpdated = (userData) => {
325
+
editingUser.value = null
326
+
// Update the local state using the composable
327
+
if (userData.id) {
328
+
updateUser(userData)
329
+
}
330
+
}
331
+
</script>
332
+
333
+
<template>
334
+
<div>
335
+
<NUsersUserForm
336
+
v-if="editingUser"
337
+
:user="editingUser"
338
+
@submit="handleUserUpdated"
339
+
/>
340
+
<NUsersList
341
+
@edit-click="handleEdit"
342
+
/>
343
+
</div>
344
+
</template>
345
+
```
346
+
347
+
**Benefits of this approach:**
348
+
-**Loose coupling** - Components don't need to know about each other's internal methods
349
+
-**Event-driven** - Clean separation of concerns
350
+
-**Reusable** - Parent can handle events however it wants
351
+
-**Testable** - Easy to test event handling
352
+
-**Flexible** - Parent can implement any update strategy
console.info('The edit event is emitted. Nuxt-users does not automatically calls the API to update the user, you should do it at the upper component.')
0 commit comments