|
Download react-query/docs/framework/angular/guides/query-functions.md from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/docs/framework/angular/guides/query-functions.md
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/docs/framework/angular/guides/query-functions.md
-
curl -L -o query-functions.md https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/docs/framework/angular/guides/query-functions.md
1.55 kB
| id: query-functions | |
| title: Query Functions | |
| ref: docs/framework/react/guides/query-functions.md | |
| [//]: # 'Example' | |
| ```ts | |
| injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchAllTodos })) | |
| injectQuery(() => ({ queryKey: ['todos', todoId], queryFn: () => fetchTodoById(todoId) }) | |
| injectQuery(() => ({ | |
| queryKey: ['todos', todoId], | |
| queryFn: async () => { | |
| const data = await fetchTodoById(todoId) | |
| return data | |
| }, | |
| })) | |
| injectQuery(() => ({ | |
| queryKey: ['todos', todoId], | |
| queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]), | |
| })) | |
| ``` | |
| [//]: # 'Example' | |
| [//]: # 'Example2' | |
| ```ts | |
| todos = injectQuery(() => ({ | |
| queryKey: ['todos', todoId()], | |
| queryFn: async () => { | |
| if (somethingGoesWrong) { | |
| throw new Error('Oh no!') | |
| } | |
| if (somethingElseGoesWrong) { | |
| return Promise.reject(new Error('Oh no!')) | |
| } | |
| return data | |
| }, | |
| })) | |
| ``` | |
| [//]: # 'Example2' | |
| [//]: # 'Example3' | |
| ```ts | |
| todos = injectQuery(() => ({ | |
| queryKey: ['todos', todoId()], | |
| queryFn: async () => { | |
| const response = await fetch('/todos/' + todoId) | |
| if (!response.ok) { | |
| throw new Error('Network response was not ok') | |
| } | |
| return response.json() | |
| }, | |
| })) | |
| ``` | |
| [//]: # 'Example3' | |
| [//]: # 'Example4' | |
| ```ts | |
| result = injectQuery(() => ({ | |
| queryKey: ['todos', { status: status(), page: page() }], | |
| queryFn: fetchTodoList, | |
| })) | |
| // Access the key, status and page variables in your query function! | |
| function fetchTodoList({ queryKey }) { | |
| const [_key, { status, page }] = queryKey | |
| return new Promise() | |
| } | |
| ``` | |
| [//]: # 'Example4' | |