|
1 | 1 | # Side effects |
2 | 2 |
|
| 3 | +Overmind helps you expose side effects to your application logic. It does this with a concept called **Providers**. A provider is nothing more than an object with methods or a class instance. For example exposing an http library like [axios](https://github.com/axios/axios) could be done like this: |
| 4 | + |
| 5 | +{% code-tabs %} |
| 6 | +{% code-tabs-item title="app/providers.js" %} |
| 7 | +```javascript |
| 8 | +export { default as axios } from 'axios' |
| 9 | +``` |
| 10 | +{% endcode-tabs-item %} |
| 11 | +{% endcode-tabs %} |
| 12 | + |
| 13 | +Or you could expose it as a class and create a custom **API**: |
| 14 | + |
| 15 | +```javascript |
| 16 | +import axios from 'axios' |
| 17 | + |
| 18 | +class Api { |
| 19 | + constructor (baseUrl) { |
| 20 | + this.baseUrl = baseUrl |
| 21 | + } |
| 22 | + get(url, ...args) { |
| 23 | + return axios.get(this.baseUrl + url, ...args) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export const api = new Api('/api/v1') |
| 28 | +``` |
| 29 | + |
| 30 | +Using a class is useful when you want to pass in options based on the environment, or maybe the library exposes a class in itself. |
| 31 | + |
| 32 | +The providers are added to the configuration of the app: |
| 33 | + |
| 34 | +{% code-tabs %} |
| 35 | +{% code-tabs-item title="app/index.js" %} |
| 36 | +```javascript |
| 37 | +import App from 'overmind/$VIEW' |
| 38 | +import state from './state' |
| 39 | +import actions from './actions' |
| 40 | +import providers from './providers' |
| 41 | + |
| 42 | +const app = new App({ |
| 43 | + state, |
| 44 | + actions, |
| 45 | + providers |
| 46 | +}) |
| 47 | + |
| 48 | +export const connect = app.connect |
| 49 | +``` |
| 50 | +{% endcode-tabs-item %} |
| 51 | + |
| 52 | +{% code-tabs-item title="app/index.ts" %} |
| 53 | +```typescript |
| 54 | +import App, { TContext, IAction, TConnect } from 'overmind/$VIEW' |
| 55 | +import state from './state' |
| 56 | +import actions from './actions' |
| 57 | +import providers from './providers' |
| 58 | + |
| 59 | +export type Context = TContext<typeof state, typeof providers> |
| 60 | + |
| 61 | +export type Action = IAction<typeof state, Context> |
| 62 | + |
| 63 | +const app = new App({ |
| 64 | + state, |
| 65 | + actions |
| 66 | +}) |
| 67 | + |
| 68 | +export type Connect = TConnect<typeof app.state, typeof app.actions> |
| 69 | + |
| 70 | +export const connect = app.connect |
| 71 | +``` |
| 72 | +{% endcode-tabs-item %} |
| 73 | +{% endcode-tabs %} |
| 74 | + |
| 75 | +The Overmind devtools will actually track any usage of these providers and give you valuable information about their execution. |
| 76 | + |
0 commit comments