You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/overmind-website/guides/beginner/05_runningsideeffects.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Running side effects
2
2
3
-
Developing applications is not only about managing state, but also managing side effects. A side effect is typically exampled with an http request or talking to local storage. In Overmind we just call this**effects**. There are several reasons why you would want to use effects:
3
+
Developing applications is not only about managing state, but also managing side effects. A typical side effect would be an HTTP request or talking to localStorage. In Overmind we just call these**effects**. There are several reasons why you would want to use effects:
4
4
5
5
1. All the code in your actions will be domain specific, no low level generic APIs
6
-
2. Your actions will have less code and you avoid leaking out things like urls, types etc.
7
-
3. You decouple the underlying tool from the usage of it, meaning that you can replace it at any time without changing your application logic
8
-
4. You can more easily expand functionality of an effect. For example you want to introduce caching or a base url to an http effect
6
+
2. Your actions will have less code and you avoid leaking out things like URLs, types etc.
7
+
3. You decouple the underlying tool from its usage, meaning that you can replace it at any time without changing your application logic
8
+
4. You can more easily expand the functionality of an effect. For example you want to introduce caching or a base URL to an HTTP effect
9
9
5. The devtool tracks its execution
10
-
6. If you write Overmind tests you can easily mock them
11
-
7. You can lazyload the effect, reducing the initial payload of the app
10
+
6. If you write Overmind tests, you can easily mock them
11
+
7. You can lazy-load the effect, reducing the initial payload of the app
12
12
13
13
## Exposing an existing tool
14
14
@@ -18,15 +18,15 @@ Let us just expose the [axios](https://github.com/axios/axios) library as an **h
We are just exporting the existing library from our effects file and include it in the application config. Now Overmind is aware of an **http** effect. It can track it for debugging and all actions and operators will have it injected.
21
+
We are just exporting the existing library from our effects file and including it in the application config. Now Overmind is aware of an **http** effect. It can track it for debugging and all actions and operators will have it injected.
22
22
23
23
Let us put it to use in an action that grabs the current user of the application.
That was basically it. As you can see we are exposing some low level details like the http method used and the url. Let us follow the encouraged way of doing things and create our own **api** effect.
29
+
That was basically it. As you can see we are exposing some low level details like the http method used and the URL. Let us follow the encouraged way of doing things and create our own **api** effect.
It can be a good idea to not allow your side effects to initialize when they are defined. This makes sure that they do not leak into tests or server side rendering. For example if you want to use Firebase. Instead of initializing the Firebase application immediately we rather do it behind an **initialize** method:
47
+
It can be a good idea to not allow your side effects to initialize when they are defined. This makes sure that they do not leak into tests or server side rendering. For example if you want to use Firebase, instead of initializing the Firebase application immediately we rather do it behind an **initialize** method:
1. We use an [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) to create a scoped internal variable to be used for that specific effect
56
56
2. We have created an **initialize** method which we can call from the Overmind **onInitialize** action, which runs when the Overmind instance is created
@@ -80,7 +80,7 @@ You can also lazily load your effects in the **initialize** method. Let us say w
We export an instance of our **Api** to the application. This allows us to also create instances in isolation for testing purposes, making sure our Api class works as we expect.
102
102
103
103
## Summary
104
-
Importing side effects directly into your code should be considered bad practice. If you think about it from an application standpoint it is kinda weird that it runs http verb methods with a string url. It is better to create an abstraction around it to make your code more consistent with the domain, and by doing so also improve maintainability.
104
+
Importing side effects directly into your code should be considered bad practice. If you think about it from an application standpoint it is kinda weird that it runs HTTP verb methods with a URL string passed in. It is better to create an abstraction around it to make your code more consistent with the domain, and by doing so also improve maintainability.
0 commit comments