# Using AppState Events

If you want to update other components when an item is Saved, Changed or Deleted then you can use the AppState class.

In your component make sure you inject the AppState first

```csharp
[Inject] public AppState AppState { get; set; } = default!;
```

### Subscribing To Events

The AppState has several events you can subscribe to in your component, these are:

```csharp
public event Func<ContentType?, string, Task>? OnContentTypeChanged;
public event Func<Content.Models.Content?, string, Task>? OnContentChanged;
public event Func<User?, string, Task>? OnUserChanged;
public event Func<Media.Models.Media?, string, Task>? OnMediaChanged;

public event Func<ContentType?, string, Task>? OnContentTypeSaved;
public event Func<Content.Models.Content?, string, Task>? OnContentSaved;
public event Func<User?, string, Task>? OnUserSaved;
public event Func<Media.Models.Media?, string, Task>? OnMediaSaved;

public event Func<ContentType?, string, Task>? OnContentTypeDeleted;
public event Func<Content.Models.Content?, string, Task>? OnContentDeleted;
public event Func<User?, string, Task>? OnUserDeleted;
public event Func<Media.Models.Media?, string, Task>? OnMediaDeleted;
```

Below is an example of subscribing to the `OnUserChanged` event and then refreshing some data on the page.

```csharp
protected override async Task OnInitializedAsync()
{
    await DataRefresh();
    AppState.OnUserChanged += HandleUsersChanged;
}

public void Dispose()
{
    AppState.OnUserChanged -= HandleUsersChanged;
}

private async Task HandleUsersChanged(User? user, string username)
{
    await DataRefresh();
    StateHasChanged();
}
```

As you can see, the item being changed or updated is passing in to most events, so you could update or check data when the events are triggered.

### Triggering The Events

There may be times where you change some data and want to notify other components, this can be done like so (Example below is notifying a user change and passing in the User being updated and the current logged in User), you would do the same for Content, Media & ContentTypes

```csharp
await AppState.NotifyUserChanged(User, AuthState.User.Identity?.Name!);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aptitude.gitbook.io/zaubercms/extending-zaubercms/using-appstate-events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
