> For the complete documentation index, see [llms.txt](https://ferp.mrbarry.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ferp.mrbarry.com/understanding-actions-in-ferp/writing-your-own-actions.md).

# Writing Your Own Actions

Actions are absolutely tied to your application, but good news, they are just basic functions, so they are easy to write. Check out this very simple action:

```javascript
const SimpleAction = (state) => [state, effects.none()];
```

Sure, it doesn't do anything, but it's an action!

## Actions with Side-Effects

{% hint style="info" %}
If you haven't already read about [Composing Custom Effects](/understanding-effects-in-ferp/custom-effects.md), go check out that section before continuing.
{% endhint %}

Up until this point, we've mostly dealt with basic actions that have no side-effects. More good news, actions always return a state and a **single** (side) effect. Yes, a valid side effect is `effects.none()`, meaning no side effect.

As you would expect from the examples so far, writing an effect into an action is simple:

```javascript
import { effects } from 'ferp';

const MyEffect = (text) => effects.thunk(() => {
  console.log('MyEffect', text);
  return effects.none();
});

const IncrementCounterAndPrint = (state) => [
  { ...state, counter: state.counter + 1 },
  MyEffect('incremented'),
];
```

## Best Practices

### Actions import Effects, Effects accept Action parameters

One thing I found immediately while writing state management with actions and effects is it is incredibly easy to have issues with circular dependencies.  For instance, often times actions can trigger effects, and those effects may trigger more actions. If your actions and effects are in separate files, how do you import things to prevent Node from having a circular dependency? The formula that works for me is letting my actions module import any and all effect modules, but forcing effect methods to have actions passed to them. Here's an example of how that can look:

{% tabs %}
{% tab title="actions.js" %}

```javascript
import { effects } from 'ferp';
import * as todoFx from './todoFx.js';

export const AddTodo = (todo) => (state) => [
  { ...state, todos: state.todos.concat(todo) },
  effects.none(),
];

export const RequestTodo = (todoId) => (state) => [
  state,
  todoApi.getTodo(state.external.fetch, todoId, AddTodo),
];
```

{% endtab %}

{% tab title="todoFx.js" %}

```javascript
import { effects } from 'ferp';

export const getTodo = (fetchFn, id, onSuccess, onFailure) => effects.defer(
  fetchFn(`https://jsonplaceholder.typicode.com/todos/${id}`)
    .then(response => response.json())
    .then(data => effects.act(onSuccess(data)))
    .catch((err) => (
      onFailure
        ? effects.act(onFailure(err))
        : effects.none()
    ))
);
```

{% endtab %}
{% endtabs %}


---

# 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 dynamically by asking a question.

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

```
GET https://ferp.mrbarry.com/understanding-actions-in-ferp/writing-your-own-actions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
