docs: move around examples
This commit is contained in:
1 parent
b9ff95fa12
commit
70cd46dbb2
45 files changed
+3742
-2291
No files matched your search
@@ -0,0 +1,58 @@
|
||||
# Asynchronous state management
|
||||
|
||||
Async is pervasive in web frontends. The web heavily relies on external resources like http, requests, storage, etc.
|
||||
|
||||
## How does Redux do it?
|
||||
|
||||
Thunks
|
||||
|
||||
- call a synchronous dispatch function
|
||||
- queues an asynchronous "thunk"
|
||||
- thunks may not modify state, may only call reducers
|
||||
|
||||
## How does Recoil do it?
|
||||
|
||||
useRecoilCallback
|
||||
|
||||
- call an async function
|
||||
- get/set any atom with the Recoil API
|
||||
- synchronously set values
|
||||
|
||||
async atoms
|
||||
|
||||
- atom's "get" returns a promise that resolves to the atom's value
|
||||
- if a request fails
|
||||
|
||||
## How should we do it?
|
||||
|
||||
```rust
|
||||
const USER_ID: Atom<String> = |atom| String::from("ThisIsDummyData");
|
||||
const TITLE: Selector<Result<String>> = |atom| {
|
||||
atom.async_get(|api| async {
|
||||
let user_id = api.get(ID);
|
||||
let resp: Result<UserData> = surf::get(format!("https://myapi.com/users/{}", user_id))
|
||||
.recv_json()
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Deref)]
|
||||
struct UserManager(RecoilApi);
|
||||
impl UserManager {
|
||||
fn load_user_data() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async fn Example(cx: Context<()>) -> Result<VNode> {
|
||||
let title = use_read(cx, TITLE)?;
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
fn use_initialize_app() {
|
||||
// initialize theme systems
|
||||
// initialize websocket connection
|
||||
// create task queue
|
||||
}
|
||||
```
|
||||
Reference in new issue
Block a user