Skip to content

Commit cd041e5

Browse files
committed
recipes/invalidate-query
1 parent 38d74ea commit cd041e5

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

docs/examples/InvalidateQuery.jsx

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { bind, Subscribe } from "@react-rxjs/core"
2+
import { createSignal } from "@react-rxjs/utils"
3+
import React, { useRef } from "react"
4+
import { concat, of } from "rxjs"
5+
import { concatMap, switchMap } from "rxjs/operators"
6+
7+
const { getTodos, postTodo } = (() => {
8+
let todos = [
9+
{
10+
id: 0,
11+
title: "Grocery shopping",
12+
},
13+
]
14+
15+
return {
16+
getTodos: async () => todos,
17+
postTodo: async (todo) => {
18+
todos = [
19+
...todos,
20+
{
21+
id: todos[todos.length - 1].id + 1,
22+
title: todo
23+
},
24+
]
25+
},
26+
}
27+
})()
28+
29+
const [todoPost$, addTodo] = createSignal()
30+
31+
const todoResult$ = todoPost$.pipe(concatMap(postTodo))
32+
33+
const [useTodos] = bind(
34+
// When do we need to request todos?
35+
concat(
36+
// 1. One single time when starting
37+
of(null),
38+
// 2. Every time we have created a new todo
39+
todoResult$
40+
).pipe(
41+
switchMap(getTodos),
42+
)
43+
)
44+
45+
function Todos() {
46+
const ref = useRef()
47+
const todos = useTodos()
48+
49+
const handleAddClick = () => {
50+
addTodo(ref.current.value);
51+
ref.current.value = ''
52+
ref.current.focus()
53+
}
54+
55+
return (
56+
<div>
57+
<input type="text" defaultValue="Do Laundry" ref={ref} />
58+
<button onClick={handleAddClick}>Add Todo</button>
59+
60+
<ul>
61+
{todos.map((todo) => (
62+
<li key={todo.id}>{todo.title}</li>
63+
))}
64+
</ul>
65+
</div>
66+
)
67+
}
68+
69+
export default function InvalidateQuery() {
70+
return (
71+
<Subscribe fallback={<div>Loading...</div>}>
72+
<Todos />
73+
</Subscribe>
74+
)
75+
}

docs/quick-start.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function CharacterCounter() {
7979
}
8080
```
8181

82-
The interactive result:
82+
### Interactive result
8383

8484
<BrowserOnly>
8585
{() => <CharacterCounter />}

docs/recipes/invalidate-query.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Invalidate Query
3+
---
4+
5+
import InvalidateQuery from "../examples/InvalidateQuery"
6+
import BrowserOnly from '@docusaurus/BrowserOnly';
7+
8+
```tsx
9+
import { bind, Subscribe } from "@react-rxjs/core"
10+
import { createSignal } from "@react-rxjs/utils"
11+
import { concat, of } from "rxjs"
12+
import { concatMap, switchMap } from "rxjs/operators"
13+
import { getTodos, postTodo } from "../my-api"
14+
15+
const [todoPost$, addTodo] = createSignal<string>()
16+
17+
const todoResult$ = todoPost$.pipe(
18+
concatMap(postTodo)
19+
)
20+
21+
const [useTodos] = bind(
22+
// When do we need to request todos?
23+
concat(
24+
// 1. One single time when starting
25+
of(null),
26+
// 2. Every time we have created a new todo
27+
todoResult$
28+
).pipe(
29+
switchMap(getTodos),
30+
)
31+
)
32+
33+
function Todos() {
34+
const ref = useRef()
35+
const todos = useTodos()
36+
37+
const handleAddClick = () => {
38+
addTodo(ref.current.value);
39+
ref.current.value = ''
40+
ref.current.focus()
41+
}
42+
43+
return (
44+
<div>
45+
<ul>
46+
{todos.map((todo) => (
47+
<li key={todo.id}>{todo.title}</li>
48+
))}
49+
</ul>
50+
51+
<button
52+
onClick={handleAddClick}
53+
>
54+
Add Todo
55+
</button>
56+
</div>
57+
)
58+
}
59+
60+
function App() {
61+
return (
62+
<Subscribe fallback={<div>Loading...</div>}>
63+
<Todos />
64+
</Subscribe>
65+
)
66+
}
67+
```
68+
69+
### Interactive result
70+
71+
<BrowserOnly>
72+
{() => <InvalidateQuery />}
73+
</BrowserOnly>

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
someSidebar: {
33
Introduction: ["motivation", "quick-start", "features"],
4+
Recipes: ["recipes/invalidate-query"],
45
"API Reference": [
56
{
67
type: "category",

0 commit comments

Comments
 (0)