Comments (9)
I'm assuming you're talking about pagination? You could do something like
const SomeComp = ({ page, pageLength }) => {
const [list, setList] = useState([])
const { get, loading } = useFetch('url')
const fetchList = async () => {
const theList = await get(`?page=${page}&pageLength=${pageLength}`)
setList(theList)
}
const mounted = useRef(false)
useEffect(() => { // componentDidMount
if (!mounted.current) {
fetchList()
mounted.current = true
}
}, [])
return <div>{list.map(item => <div>{item.text}</div>)}<div>
}
Does this make sense? Or are you asking something else?
from use-http.
Sorry for being unclear. What I actually am trying to do is load multiple URLs at once:
const SomeComp = ({urls}) => {
const images = // fetch all the urls
return images.map(/* do something with the results*/)
}
However I understand now that I can probably solve it like this:
const [results, setResults] = useState([])
const request = useFetch()
async function fetchStuff() {
for (const url of urls) {
const result = await request.get(url)
setResults([...results, result])
}
}
useEffect(() => { fetchStuff() })
from use-http.
You can do that, however it's always recommended that you don't have api calls/awaits
in loops. Why do you need to make an api call for image urls? If you're displaying images, why not just do imageUrls.map(url => <img src={url} />)
from use-http.
In my application I am fetching data, not images – that was just for the example.
from use-http.
You could do something like
const Comp = (props) => {
const [imagesData, setImagesData] = useState([])
const [loading, setLoading] = useState(false)
const { get } = useFetch('url')
const getImageData = useCallback(async () => {
setLoading(true)
const imgData = await images.map(async image => await get('/image?id=${image.id}'))
setImagesData(imgData)
setLoading(false)
}, [props.images, imagesData, setImagesData, setLoading])
const mounted = useRef(false)
useEffect(() => {
if (!mounted.current) { // on mount
getImagesData()
}
}, [getImagesData])
return (
<>
{loading && 'Loading...'}
{imagesData.length > 0 && imagesData.map(img => <img src={img.url} />)}
</>
)
}
Is this something like what you're looking for?
from use-http.
@supermihi is your question answered? Gonna close this if it is :)
from use-http.
@alex-cory thanks for your answer. The above should work (though I'd remove the mounted
construct, which would prevent re-loading if props.images
changes, right?).
However I don't really see the benefit of useFetch
here, as opposed to calling fetch
directly?
from use-http.
Oh yeah, that makes sense about the mounted
part. In this case, the only benefit would be if you used the <Provider url='https://example.com'><App /></Provider>
. Kind of like how axios works. Also, if you have authentication, you could handle that at the top level in addition to the syntax is a bit cleaner. Having the global fetch config is pretty nice.
from use-http.
Ok, thanks a lot!
from use-http.
Related Issues (20)
- useFetch `post` call inserts quotes when passed a string HOT 6
- React Native and Cache
- [Feature Request] time denounce http get requests
- does useFetch have option for skip request?
- Data not populated at mount HOT 5
- React 18 StrictMode HOT 22
- `Error: Invalid hook call` after upgrading to React 18.1 HOT 1
- Interceptors aren't chained HOT 8
- Cannot abort delayed retries
- use-Fetch Post is having issue with csrf headers HOT 7
- [Feature Request] A way to invalidate the cache HOT 1
- Upload File Progress
- can recall post when parameter change?
- does post can auto fire? HOT 2
- Eslint rule react-hooks/exhaustive-deps questions on having response as depency HOT 1
- React 18 not supported? HOT 1
- Following instructions in .github/contributing.md fails when using the latest React 18 HOT 2
- Initial URL with a trailing slash adds double slash on relative urls
- TS: Ability to add expected type for fetch body. HOT 1
- [Feature Request] Set cacheLife per request?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-http.