-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Dependency injection | ||
|
||
## Explanation | ||
|
||
Dependency injection is a concept for reuse some dependencies based on the running context. Good for Reatom users - all basic entities (actions, atoms) are already working in a controlled context - a store. By the same token, any atom dependencies (as another atom) are automatically adding to a store when computed atom connecting to it. That means you already have dependencies resolving mechanism with Reatom. | ||
|
||
What is it mean in the conclusion? You can create not only reactive atoms (depended on actions) but just _static_ atoms and use it as a service. | ||
|
||
## Example | ||
|
||
> IMPORTANT NOTE: in order to prevent errors in serialization **use a symbol** as an atom key | ||
```ts | ||
/* DECLARE API SERVICE */ | ||
|
||
import { declareAtom, declareAction, createStore } from '@reatom/core' | ||
import axios from 'axios' | ||
|
||
export const API = Symbol('api') | ||
export const apiAtom = declareAtom(API, axios, () => []) | ||
|
||
/* SOME FEATURE CODE */ | ||
|
||
export const recieveData = declareAction() | ||
export const requestData = declareAction(async (payload, store) => { | ||
const data = await store.getState(apiAtom).get(url, payload) | ||
store.dispach(recieveData(data)) | ||
}) | ||
|
||
/* TESTS */ | ||
|
||
// here we rewrite initial state (axios) of apiAtom by preloaded state | ||
const store = createStore({ | ||
[API]: mockedAxios | ||
}) | ||
// ... | ||
store.dispatch(requestData()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters