ferp-js
Search…
What is a Ferp Action?

Actions

Actions are the only way to initiate new state changes and side-effects in Ferp. State changes must be complete and immutable, and side effects must always be declared, even if there are no side-effects. Actions are always pure functions that return [state, sideEffect], and pure meaning it does not use any data not immediately accessible to the action. On the contrary, an invalid action would use things like Math.random() or Date.now(), which are external to the action function.

Types of Actions

Naked Action

Plain, or naked, actions are actions that need no parameters. For instance, a naked action could be something that only updates a single state property by incrementing it by 1. Let's see what that looks like:
1
import { effects } from 'ferp';
2
3
const IncrementCounterByOne = (state) => [
4
{ ...state, counter: state.counter + 1 },
5
effects.none(),
6
];
Copied!
Depending on the shape of your state, you may find naked actions to be very fitting.
There are two ways to call an action, and here they are:
1
import { app, effects } from 'ferp';
2
3
const IncrementCounterByOne = (state) => [
4
{ ...state, counter: state.counter + 1 },
5
effects.none(),
6
];
7
8
const dispatch = app({
9
init: [
10
{ counter: 0 },
11
effects.act(IncrementCounterByOne), // run an action via effect
12
],
13
});
14
15
dispatch(IncrementCounterByOne); // run an action via dispatch
Copied!

Action Builder

Action builders are actions that can take parameters. This may mean a value can be passed from user input, or from some other external side-effect. Using the previous counter example, here's what it would look like if it took external input for the increment amount:
1
import { effects } from 'ferp';
2
3
const IncrementCounterByN = (n) => (state) => [
4
{ ...state, counter: state.counter + n },
5
effects.none(),
6
];
Copied!
Similar to naked actions, there are two ways to run this action in your application:
1
import { app, effects } from 'ferp';
2
3
const IncrementCounterByN = (n) => (state) => [
4
{ ...state, counter: state.counter + n },
5
effects.none(),
6
];
7
8
const dispatch = app({
9
init: [
10
{ counter: 0 },
11
effects.act(IncrementCounterByN(10)), // run a builder action via effect
12
],
13
});
14
15
dispatch(IncrementCounterByN(999)); // run a builder action via dispatch
Copied!
If you're not sure how the builder works, this is just a function that returns a function. Here's probably a more familiar version to read:
1
import { effects } from 'ferp';
2
3
function IncrementCounterByNBuilder(n) {
4
return function IncrementCounterByNAction(state) {
5
return [
6
{ ...state, counter: state.counter + n },
7
effects.none(),
8
];
9
};
10
};
Copied!
This is called a closure, and it's used in Javascript to encapsulate a scope. For instance, the variable n is available in the inner function (IncrementCounterByNAction), but n is not available outside of this closure (IncrementCounterByNBuilder).
To make this easier to understand, once we have this builder that returns an action, internally, Ferp does something like this:
1
// You call...
2
dispatch(IncrementCounterByNBuilder, 999);
3
4
// Ferp does...
5
const yourAction = IncrementByNBuilder(999);
6
dispatch(yourAction);
Copied!
Last modified 3mo ago