-
-
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
5 changed files
with
72 additions
and
12 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 @@ | ||
# Dependencie injection | ||
|
||
## Explanation | ||
|
||
Dependencie injections is a concept for reuse some dependencies based on the running context. Good for Reatom users - all basic entities (actions, atoms) is a already work in controlled context - a store. By the same token any atom dependencies (as an other atoms) are autamaticaly adding to a store when computed atom connecting to it. That means you already has dependencies resoving mechanism with Reatom. | ||
|
||
What is it mean in conclusion? You can create not only reactive atoms (depended from actions) but just _static_ atoms and use it as a services. | ||
|
||
## Example | ||
|
||
> IMPORTANT NOTE: in order to prevent errors in serelisation **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