
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
react-use-data-hook
Advanced tools
Reusable hook for data retrieval. Can Fetch on mount (optionally) and refetch.
Just inject a function that returns a Promise with your data.
npm i react-use-data-hook
yarn add react-use-data-hook
const getDataById = (id) => Promise.resolve('Some data')
import useDataHook from 'react-use-data-hook'
function MyComponent({id}){
const { data, loading, error, refetch } = useDataHook(getDataById, id);
return (
<>
{loading && <span>Loading</span>}
{!loading && <>
<span>{data}</span>
<button onClick={()=>refetch(id)}>Click</button>
</>
}
{error && <span>{error}</span>}
</>
)
}
const options = {
fn: () => Promise.resolve({})),
initialFetch: false,
debug: false,
default: []
}
const { data, loading, error, refetch } = useDataHook(options, asyncParameters);
The useDataHook has to be called with one or more parameters.
Options can either be an options object or an async function to be called. The asyncParameters can be one or more arguments that will be used to call the async function.
// example 1: using a function and multiple arguments
const { data, loading, error, refetch } = useDataHook(getUsersById, userOneId, userTwoId);
// example 2: using an options object and no arguments
const options = { fn: getAllUsers, initialFetch: false, debug: true }
const { data, loading, error, refetch } = useDataHook(options);
Property name | Default | Required | Description |
---|---|---|---|
fn | undefined | true | Async function that returns a Promise |
initialFetch | false | false | Should a request be made on mount |
debug | false | false | Console log debug information |
default | null | false | optional default value |
Look at the example in the ./example folder for a type-ahead input field using this hook.
Hope you like this repo and find it useful. If you have any comments or questions, please open a ticket or a PR.
FAQs
Hook to fetch and re-fetch data from an api service
The npm package react-use-data-hook receives a total of 32 weekly downloads. As such, react-use-data-hook popularity was classified as not popular.
We found that react-use-data-hook 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.