Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
clean-fetch
Advanced tools
yarn add clean-fetch
This library is client side only, it does not support server side rendering.
import {useFetch} from 'clean-fetch'
Argument: useFetch
takes only one argument, a function that returns a promise which resolves to the data. Note: the library does not support if the function return the data synchronously.
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.
For multiple renders, render
uses the latest function passed to the hook.useFetch
: only fetches data in the first return. 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 reference never changes
useHook<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 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
}
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
data
and Error
's types are defined. We will improve the type definition in the future.import {Fetch} from 'clean-fetch'
return <Fetch fetch={() => fetchJson('/user/info')}>
{(data, reload) => <Data data={data} reload={reload}/>}
</Fetch>
Fetch
is a React component which takes 3 props:
Fallback
: a component that takes an optional error
prop and a reload
prop which is a function that reloads the data.
If error
is undefined, it means the data is loading.
By default, it is a component which returns null
.fetch
: a function that returns a promise which resolves to the data.children
: a function that takes 2 arguments: the data and a reload
function and returns a ReactNode.We provide 2 wrappers of browser fetch API: fetchJson
and fetchText
.
import {fetchJson, fetchText, setFetchErrorHandler} from 'clean-fetch'
fetchJson
and fetchText
takes the same arguments as browser fetch API.
The former returns a promise which resolves to the JSON data, the latter returns a promise which resolves to the text.
If the response is not ok, the promise will be rejected with an Error object whose message
is interpreted as below:
error
or message
property, the error
(or message
) property will be used as the error message.setFetchErrorHandler
, a function that takes a function that takes the response object and should throw an Error object.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 8 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.