> 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 %}
