> For the complete documentation index, see [llms.txt](https://aurelia-1.gitbook.io/v1-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aurelia-1.gitbook.io/v1-docs/aurelia-packages/store/what-are-actions.md).

# What are actions?

Actions are the only way to modify the application's state in the Aurelia Store plugin. They are essentially functions that take the current state and optionally some arguments, and return a new state.

### Immutability

It is crucial that actions **do not mutate** the existing state object. Instead, they should create a new state object with the desired changes. This ensures that the state remains predictable and that we can easily track changes over time.

### Example Action

Let's create an action that adds a new framework to the `frameworks` array in our state:

```typescript
// actions.ts
import { State } from './state';

export function addFramework(currentState: State, newFramework: string): State {
  return {
    ...currentState, // Copy the existing state
    frameworks: [...currentState.frameworks, newFramework] // Create a new array with the added framework
  };
}
```

**Explanation:**

1. **`export function addFramework(...)`**: Defines a function called `addFramework` that takes two arguments:
   * `currentState: State`: The current state object.
   * `newFramework: string`: The name of the new framework to add.
2. **`return { ...currentState, ... };`**: This uses the object spread syntax to create a new object that is a copy of the `currentState`.
3. **`frameworks: [...currentState.frameworks, newFramework]`**: This creates a new array that contains all the elements of the existing `frameworks` array, plus the `newFramework` appended to the end. This ensures that we don't modify the original `frameworks` array.

### Registering Actions

Before you can dispatch an action, you need to register it with the store using the `store.registerAction` method. This is typically done in the constructor of your component or service where you want to use the action:

```typescript
// app.ts
import { inject } from 'aurelia-dependency-injection';
import { Store } from 'aurelia-store';
import { State } from './state';
import { addFramework } from './actions';

@inject(Store)
export class App {
  constructor(private store: Store<State>) {
    this.store.registerAction('AddFramework', addFramework);
  }

  // ...
}
```

**Explanation:**

1. **`import { addFramework } from './actions';`**: Imports the `addFramework` action.
2. **`this.store.registerAction('AddFramework', addFramework);`**:
   * `'AddFramework'` is a string identifier for the action. This is useful for debugging and logging.
   * `addFramework` is the actual action function.

### Unregistering Actions

You can unregister an action using the `store.unregisterAction` method:

```typescript
this.store.unregisterAction(addFramework);
```

This removes the action from the store's registry.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://aurelia-1.gitbook.io/v1-docs/aurelia-packages/store/what-are-actions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
