@@ -2,34 +2,28 @@ module Fetch = {
22 type response
33
44 @send external json : response => Js .Promise .t <'a > = "json"
5- @val external fetch : ( string , { .. }) => Js .Promise .t <response > = "fetch"
5+ @val external fetch : string => Js .Promise .t <response > = "fetch"
66}
77
88type todo = {id : string , title : string }
99
1010let apiUrl = "https://jsonplaceholder.typicode.com/todos/1"
11- let client = ReactQuery .Provider .createClient ()
1211
1312let fetchTodos = (_ ): Js .Promise .t <todo > => {
14- let {then } = module (Promise )
15-
16- Fetch .fetch (
17- apiUrl ,
18- {
19- "method" : "GET" ,
20- },
21- )-> then (Fetch .json )
13+ Fetch .fetch (apiUrl )-> Promise .then (Fetch .json )
2214}
2315
2416module TodoItem = {
25- let {useQuery , queryOptions } = module (ReactQuery )
26-
2717 @react.component
2818 let make = () => {
29- let queryResult = useQuery (
30- queryOptions (
19+ let queryResult = ReactQuery . useQuery (
20+ ReactQuery . queryOptions (
3121 ~queryFn = fetchTodos ,
3222 ~queryKey = "todos" ,
23+ /*
24+ * Helper functions to convert unsupported TypeScript types in ReScript
25+ * Check out the module ReactQuery_Utils.res
26+ */
3327 ~refetchOnWindowFocus = ReactQuery .refetchOnWindowFocus (#bool (false )),
3428 (),
3529 ),
@@ -38,13 +32,19 @@ module TodoItem = {
3832 <div >
3933 {switch queryResult {
4034 | {isLoading : true } => "Loading..." -> React .string
41- | {data : Some (todo )} => ` Todo Title ${todo.title}`-> React .string
35+ | {data : Some (todo ), isLoading : false , isError : false } =>
36+ ` Todo Title ${todo.title}`-> React .string
4237 | _ => ` Unexpected error...`-> React .string
4338 }}
4439 </div >
4540 }
4641}
4742
43+ /*
44+ * Create a new client
45+ */
46+ let client = ReactQuery .Provider .createClient ()
47+
4848@react.component
4949let make = () => {
5050 <ReactQuery .Provider client >
0 commit comments