- a react component which automatically renders childs depending on returned data
7
7
- i.e. also maintains a state of particular set of resources
@@ -12,6 +12,9 @@ Under development
12
12
- clean up unused resources
13
13
- better than connect/thunk/request pattern
14
14
- use `Query` as the "container", no need to dis actions for API requests
15
+
- how to handle ssr?
16
+
- refresh original/current request
17
+
- load another url
15
18
16
19
```
17
20
<Query>
@@ -23,12 +26,130 @@ Under development
23
26
24
27
## Usage Example
25
28
26
-
```js
29
+
In the most simple example, you just drop `Query` into your component hierarchy where you need the data. Consider a page that lists all your blog posts, you render a header and footer, and query for the actual posts themselves.
A very popular way to divide responsibilities in React apps is the [container/presenter pattern](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0). This is potentially a very clean pattern to use with our `Query` component if you include this in your container component:
`Query` has the ability to cache the result set of executed requests in memory. The caching strategy is very simple, as the result set is stored in a cache key that corresponds to `links.self` from the json:api body, falling back to the provided `endpoint` if `links.self` is not present.
98
+
99
+
If you would like to use the caching mechanism it is highly encouraged that `links.self` is a permalink for all cached requests. This is important to avoid overlap or missing resources, see this detailed below. To opt-in set boolean prop `enableCache` on your `Query` instance; `<Query endpoint="..." enableCache />`
100
+
101
+
### Why Permalinks are Important
102
+
103
+
Let's use above example with blog posts. When a user initially loads our page, we will request `/blog-posts` to retrieve the latest blog posts. We will also add buttons to go to next and prev pages:
This is an example response body for above code example, that will be error prone when cached. Let's say a user is vieweing the first page on your website, meanwhile you're creating a newblogpost. When the user loads page 2, they will see the last blog post from page 1, on page 2 as well. Not only is this error prone when cached, but it's particularly bad because the result set from the first page is cached and will not refresh to the user.
122
+
123
+
```json
124
+
GET /blog-posts
125
+
126
+
{
127
+
"links": {
128
+
"self": "/blog-posts",
129
+
"next": "/blog-posts?page=2"
130
+
},
131
+
"data": {
132
+
"...": {}
133
+
}
134
+
}
135
+
```
136
+
137
+
#### Good Response
138
+
139
+
A response that would work well and prevent overlapping resources, uses some form of permalinking to a particular result set:
140
+
141
+
```json
142
+
GET /blog-posts
143
+
144
+
{
145
+
"links": {
146
+
"self": "/blog-posts?fromTimestamp=1445412480",
147
+
"next": "/blog-posts?fromTimestamp=499165200"
148
+
},
149
+
"data": {
150
+
"...": {}
151
+
}
152
+
}
153
+
```
154
+
155
+
In this case `links.self` does not reflect the requested URL, instead the server is set up to provide a link that is guaranteed to return the same result set. In this case the strategy is to use the timestamp of the latest blog post. This makes it a far more reliable key for our caching mechanism.
0 commit comments