Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
clean-fetch
Advanced tools
This library is deprecated. Please use `useAsync()` from [misc-hooks](https://www.npmjs.com/package/misc-hooks) instead. # Clean Fetch
This library is deprecated. Please use useAsync()
from misc-hooks instead.
npm i clean-fetch
import useFetch from 'clean-fetch'
Signature: useFetch<T>(fetchfn, getInitial)
.
fetchFn: () => Promise<T> | T
: a function that returns the data or a promise which resolves to the data.
getInitial?: () => T | undefined
: an optional function that returns the initial data.
If not provided, the initial data is undefined
.
getInitial()
can return undefined
, getInitial
can be absent, or it can throw an error.
Returns {data, error, reload}
where:
data
and error
are both undefined
, it means the data is loading or not yet fetched (initial render).
They are never both not undefined
.reload
: a function that takes no argument, reloads the data and returns what the function passed to the hook returns.
The reload
function reference never changes, you can safely pass it to the independent array of useEffect
without causing additional renders.
In subsequent renders, reload
uses the latest function passed to the hook.useFetch
: only fetches data in the first return, only if initial data is not provided.
If you want to refetch the data, you need to manually call reload()
.
const {data, error, reload} = useFetch(() => fetchData(params))
// when params changes, you need to manually call reload()
useEffect(() => void reload(), [params, reload]) // `reload` value never changes
useFetch<T>()
has a generic type T
which is the type of the data returned by the function passed to the hook.reload()
, error
and data
are immediately/synchronously set to undefined
(via setState
) and the data is refetched.// only update when value is not undefined
export const useKeep = <T>(value: T): T => {
const ref = useRef(value)
if (value !== undefined) ref.current = value
return value ?? ref.current
}
This useKeep
hook is available in misc-hooks package.
export const useTimedOut = (timeout: number) => {
const [timedOut, enable] = useReducer(() => true, false)
useEffect(() => {
let cancelled = false
const timer = setTimeout(() => !cancelled && enable(), timeout)
return () => {
cancelled = true
clearTimeout(timer)
}
}, [enable, timeout])
return timedOut
}
const {data, error, reload} = useFetch(() => fetchData(params))
const timedOut = useTimedOut(500)
return error // has error
? <ErrorPage/>
: data // has data
? <Data data={data}/>
: timedOut // loading
? <Loading/>
: null
This useTimedOut
hook is available in misc-hooks package.
data
and Error
's types are defined. We will improve the type definition in the future.FAQs
This library is deprecated. Please use `useAsync()` from [misc-hooks](https://www.npmjs.com/package/misc-hooks) instead. # Clean Fetch
The npm package clean-fetch receives a total of 3 weekly downloads. As such, clean-fetch popularity was classified as not popular.
We found that clean-fetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.