# 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!);
```

