
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@signal-noise/use-api
Advanced tools
A simple react hook for loading data from an api with polling support.
If you need a simple way to load data quickly this is for you. It allows you to very easily request data from any endpoint and have it avaliable in your React application.
You can optionally specify a polling interval and manually trigger a refresh. It also gracefully cancels any open requests if you decide to change the URL and restarts timers if the polling interval changes.
const { data, loading, changed, error, refresh } = useApi(
"https://some-api.com",
10000
);
Install this package with npm
.
npm i @signal-noise/use-api
Here is an example of a GET
api call to retrieve a list of people which polls every 10 seconds, with a manual refresh button.
import React from 'react';
import useApi from '@signal-noise/use-api';
import PeopleList from './PeopleList';
const PeopleList = () = {
const { data, loading, error, refresh } = useApi("https://some-api.com", 10000);
const people = data.people || [];
return (
<>
{loading && <p>Loading...</p>}
{error && <p>{error}</p>}
<button onClick={refresh} disabled={loading}>Refresh</button>
<PeopleList people={people} />
</>
);
}
You can optionally pass in data and specify the request type. Below is a minimal example of a user search UI. (You may wish to debounce the user input 🤷)
import React, { useState } from 'react';
import useApi from '@signal-noise/use-api';
import PeopleList from './PeopleList';
const PeopleSearch = () = {
const [keywords, setKeywords] = useState("kazumi");
const { data } = useApi("https://some-api.com", 0, { keywords }, "post");
const people = data.people || [];
return (
<>
<input value={keywords} onChange={e=>setKeywords(e.target.value)} />
<PeopleList people={data.people} />
</>
);
}
apiEndpoint
- A URL to request data from.pollInterval
- How often to re-request updated data. Pass 0 to disable polling (the default behaviour).payload
- A data object to send in the request. If we are performing a GET request, it is appended into the querystring (e.g. ?keywords=hello
). If it is a POST request it is sent in the request body as JSON.method
- Set the request type, either get
or post
. (defaults to get
)changed
: A function that is called if the data actually changed during the request. If this is specified, use-api does extra checking and compares old and new data. If data does not change, new data is not propagated and a redraw is saved. Please note, this may have performance repercussions if the data is large as it performs a deep comparison between new and old data to determine if they are equivalent.data
: The data returned from the API.loading
: A boolean signifying if the data is currently being loaded.error
: A string representation of an error if it occurred during loading.refresh
: A function to call to re-request the data.0.2.0
changed
callback argument.FAQs
A react hook to load data from a simple api.
The npm package @signal-noise/use-api receives a total of 5 weekly downloads. As such, @signal-noise/use-api popularity was classified as not popular.
We found that @signal-noise/use-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.