-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standard way to fetch from URL #806
Comments
@p-netm @RayceeM for some reason Github will not let me assign you here. |
yeah, it makes sense that we should standardize this. I like it. The only concerns i might raise are: For some of the components we have factored out the data loading functions to utils files, which I think is nice(for Reveal-fe). I am not sure how the pattern for using setLoading in these functions would look like. I guess the easiest would be to pass an optional stateSetter function to the data-loading function. (PS: consider EdgeCases where component makes use of 2 or more of such data-loading functions!!)
I know this is more of a draft, but we need to consider that this might result in the |
@p-netm How would you fix it in the example component above? |
I also believe we would need to do something like |
@p-netm maybe it would be best to write up a quick demo component that does this in a comment here? |
ok. |
import React, { useEffect, useRef, useState } from 'react';
export const NiceComponent = () => {
// we start out in a "loading" state
const [loading, setLoading] = useState<boolean>(true);
const isMounted = useRef<boolean>(false);
async function loadData() {
try {
// do your fetch here to get the data
await fetch('https://exmaple.com/foobar').then(response => {
if (!response) {
// this means we got something false-ey from the fetch and we need
// to deal with it
}
// do something with the valid response
});
} catch (error) {
// do something with the error
console.log(error);
} finally {
// set loading to false because in one way or another we have fetched
// from the URL and either gotten the data or an error
// in any case, we are not "loading" any more
if (isMounted.current) {
setLoading(false);
}
}
}
useEffect(() => {
isMounted.current = true;
loadData().catch(error => {
// do something with the error
console.log(error);
});
return () => {
isMounted.current = false;
};
}, []);
if (loading === true) {
return <p>this is the loading message/indicator</p>;
}
// finally we render whatever we want
return <p>something</p>;
}; |
@moshthepitt |
@p-netm sounds fantastic. We should try and use it and see. |
Good find @p-netm, this looks cool and seems like it can offer us a lot!
|
I have noticed that we have different ways, and probably differing views on how to deal with components that fetch data from some URL, and then display it.
In dealing with such components, we need to consider:
I think that one way of dealing with this is to structure the component like this:
Does anyone have a different opinion, or is there something that I have missed? Let us discuss and come up with a standard way of doing it.
The text was updated successfully, but these errors were encountered: