UNPKG

launch.io

Version:

Launch.IO is an Ultra Hip, Simple, and Fast, Time Traveling React State Management Library

56 lines (34 loc) 1.8 kB
# ![Launch.IO Logo](../../logo/logo-small.png) Service Back to [API Reference](./index.md). --- Please be sure to have reviewed the [terminology](./terminology.md) before reviewing the service documentation below. Each service object must contain `name` (**string**), `initialState` (**object**), and `actions` (**object**) properties. ### Properties `name` **string** The name of the service and it defines how to access `state` for this service from the [`useLaunch`](./useLaunch.md) 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`](./useLaunch.md) 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`](./asyncExample.md). The `payload` argument is an `object` that is passed into the launch action. ### Tips 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](./asyncExample.md). ### Example ```javascript const calculatorService = { name: "calculator", initialState: { value: 0, }, actions: { increase: ({ state, actions }, payload) => ({ value: state.value + payload, }), decrease: ({ state, actions }, payload) => ({ value: state.value - payload, }), }, }; ```