{"id":16487158,"url":"https://github.com/phalt/usequery","last_synced_at":"2026-01-20T00:40:59.327Z","repository":{"id":43993648,"uuid":"240166534","full_name":"phalt/usequery","owner":"phalt","description":"A React hook for making REST API calls","archived":false,"fork":false,"pushed_at":"2023-01-05T07:15:10.000Z","size":885,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T23:42:36.451Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phalt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-13T03:11:59.000Z","updated_at":"2022-02-19T15:34:07.000Z","dependencies_parsed_at":"2023-02-03T17:25:12.042Z","dependency_job_id":null,"html_url":"https://github.com/phalt/usequery","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalt%2Fusequery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalt%2Fusequery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalt%2Fusequery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalt%2Fusequery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phalt","download_url":"https://codeload.github.com/phalt/usequery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247792924,"owners_count":20996896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T13:32:56.254Z","updated_at":"2026-01-20T00:40:59.295Z","avatar_url":"https://github.com/phalt.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useQuery\n\nA simple React hook and component for handling REST API calls.\n\nProvides loading state, success state, and error states.\n\n## useQuery(url, options, deserialize)\n\nRequires a `url` parameter.\n\nIf `options` not supplied, defaults to an empty object. Put your `fetchOptions` here.\n\nArgument `deserialize` is a callback that takes [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) from the fetch result. Default is `res =\u003e res.json()`.\n\n### Usage\n\n```js\nimport React, { Fragment } from \"react\";\nimport { useQuery } from '@phalt/usequery';\n\nexport const myComponent = () =\u003e {\n    const [{ data, loading, error }, { refetch, reset }] = useQuery({\n        url: \"https://pokeapi.co/api\",\n        { headers: { accept: \"application/json\" }}\n    });\n\n    return (\n        \u003cFragment\u003e\n            \u003cH2\u003eMy query\u003c/h2\u003e\n            \u003cButton onClick={refetch}\u003eRefresh\u003c/Button\u003e\n            \u003cButton onClick={reset}\u003eReset state\u003c/Button\u003e\n            { loading \u0026\u0026 \u003ch2\u003eLoading!\u003c/h2\u003e }\n            { error \u0026\u0026 \u003ch2\u003eSomething went wrong!\u003c/h2\u003e }\n            { data \u0026\u0026 \u003ch3\u003e{data}\u003c/h3\u003e }\n        \u003c/Fragment\u003e\n    );\n};\n\n```\n\n## POST requests\n\nYou can use the `useQuery` hook to do this if you want full control.\n\n```js\nimport { useQuery } from '@phalt/usequery';\n\nconst httpOptions = {\n    headers: { \"Content-Type\": \"application/json\" },\n    method: \"POST\",\n    body: JSON.stringify(myData)\n};\n\nconst [{ data, loading, error }, { refetch, reset }] = useQuery({\n        url: \"https://example.com/resource\",\n        httOptions\n    });\n\n```\n\n## APIQuery component\n\nFor convenience we ship an APIQuery component:\n\n```js\nimport { APIQuery } from \"@phalt/usequery\";\n\nconst myComponent = () =\u003e {\nreturn (\u003cAPIQuery\n    ErrorState={props =\u003e \u003cp\u003e{props.error}\u003c/p\u003e}\n    LoadingState={() =\u003e \u003cp\u003eLoading...\u003c/p\u003e}\n    SuccessState={props =\u003e \u003cp\u003e{props.data}\u003c/p\u003e}\n    path=\"https://myapi.com/api/v1/foo\"\n/\u003e)\n}\n```\n\nSee full example of in the \"uploadin JSON data\" example [here](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphalt%2Fusequery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphalt%2Fusequery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphalt%2Fusequery/lists"}