
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-use-state-async
Advanced tools
A custom hook trigger call async function each dependencies changes. Support holding and updating for fetch data.
A custom hook trigger call async function each dependencies changes. Support holding and updating for fetch data.
With npm
npm install --save react-use-state-async
With yarn
yarn add react-use-state-async
import * as React from "react";
import useStateAsync from "react-use-state-async";
export default function App() {
const [url, setUrl] = React.useState("");
const { isLoading, data, error, setData, fetch } = useStateAsync(() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve("api data");
}, 1000);
});
}, [url]);
return (
<div>
<button onClick={() => setUrl("https://urlapi.com")}>Change url</button>
<button onClick={() => fetch()}>Refetch api</button>
<button onClick={() => setData("new data")}>Update data</button>
{isLoading ? <p>Loading</p> : <p>Loaded</p>}
{data && <p>{data}</p>}
{error && <p>{error}</p>}
</div>
);
}
useStateAsync(
callback: () => any | async () => Promise<any>,
dependencies: Array<any>
)
parameter | type | description |
---|---|---|
isLoading | boolean | `true` if the callback function is running |
data | any | the data is returned in callback function |
error | any | the error is thrown in callback function |
setData | function | update `data` state |
fetch | function | trigger call callback function |
FAQs
A custom hook trigger call async function each dependencies changes. Support holding and updating for fetch data.
The npm package react-use-state-async receives a total of 2 weekly downloads. As such, react-use-state-async popularity was classified as not popular.
We found that react-use-state-async demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.