Back to API Reference.
Please be sure to have reviewed the terminology before reviewing the service documentation below.
Each service object must contain name
(string), initialState
(object), and actions
(object) properties.
name
string
The name of the service and it defines how to access state
for this service from the useLaunch
hook.
initialState
object
The beginning state for the service.
actions
object
Service actions functions
that can be called to change the state for the service. A service function receives context
and payload
arguments and it must return a new service state object.
The context
argument is an object
that consists of state
, and actions
properties. These are the same types that are returned from the useLaunch
hook except that they are scoped to the local service. Service actions can be launch other launch actions. An example of this can be found in the async example
.
The payload
argument is an object
that is passed into the launch action.
It is recommended that your service functions do not mutate state. Instead they should return a brand new state object.
Service actions are able to launch actions and an example of this can be found in the async example.
const calculatorService = {
name: "calculator",
initialState: {
value: 0,
},
actions: {
increase: ({ state, actions }, payload) => ({
value: state.value + payload,
}),
decrease: ({ state, actions }, payload) => ({
value: state.value - payload,
}),
},
};