Gathering detailed insights and metrics for @bloodyaugust/use-fetch
Gathering detailed insights and metrics for @bloodyaugust/use-fetch
npm install @bloodyaugust/use-fetch
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
105,698
Last Day
1
Last Week
4
Last Month
57
Last Year
4,093
5 Commits
3 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.1
Package Id
@bloodyaugust/use-fetch@1.1.1
Unpacked Size
18.30 kB
Size
6.37 kB
File Count
11
NPM Version
8.15.0
Node Version
16.17.0
Publised On
30 Aug 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-90.7%
4
Compared to previous week
Last month
307.1%
57
Compared to previous month
Last year
-89.2%
4,093
Compared to previous year
A simple, safe fetch custom hook for React. Why safe? There's a good chance you've seen this before:
Warning: Can’t perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
This hook helps you avoid mutating state on components that are unmounted
by providing you with the mounted
state of the hook, and aborts in-flight requests on unmount
.
unmounted
components.Context
s were harmed (or used) in the source of this library.response
, the json
, and of course the mounted
state.json
parsing for those one-off cases.Promise
-based API.unmounted
.Promise
if the response
indicates failure.React
.1npm install @bloodyaugust/use-fetch 2# or, if using yarn 3yarn add @bloodyaugust/use-fetch
This example demonstrates how you might get some todos with useFetch
while being sure you don't mutate state after your component is unmounted
.
1import React, { useEffect, useState } from "react"; 2import useFetch from "@bloodyaugust/use-fetch"; 3 4function TodoList() { 5 const { execute } = useFetch(); 6 const [todos, setTodos] = useState([]); 7 8 useEffect(() => { 9 const getTodos = async () => { 10 await execute("https://jsonplaceholder.typicode.com/todos/").then( 11 ({ json, mounted }) => { 12 if (mounted) { 13 setTodos(json); 14 } 15 } 16 ); 17 }; 18 19 getTodos(); 20 }, []); 21 22 return ( 23 <div> 24 {todos.map((todo) => { 25 <span key={todo.id}>{todo.title}</span>; 26 })} 27 </div> 28 ); 29}
1useFetch({
2 noJSON: bool, // Set true if you want to skip JSON parsing (no json key will be provided to the promise chain)
3});
execute
is the function returned for invoking your fetch. It provides as consistent an API as it can, but there are some slight differences depending on if the request is successful, aborted, or failed for some other reason.
Parameters are the same as fetch.
1useFetch('https://jsonplaceholder.typicode.com/todos/') 2 .then({ 3 response, // The fetch Response object 4 json, // The result of response.json() (not present if the noJSON prop was set) 5 mounted // The mounted state of the hook 6 } => {})
1useFetch('https://jsonplaceholder.typicode.com/todos/')
2 .catch({
3 error // The Error object thrown by fetch
4 } => {})
1useFetch('https://jsonplaceholder.typicode.com/todos/')
2 .catch({
3 response,
4 error, // new Error(`Request failed: ${response.status}`)
5 mounted
6 } => {})
completed
is a ref returned from the hook to allow for determining the completion state of the fetch. It starts as false
, but becomes true when the request completes for any reason (including abort).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
28 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More