-
Notifications
You must be signed in to change notification settings - Fork 109
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
data set to {} between re-fetches #117
Comments
I generally store the data in another state variable then update it in a useEffect that checks if data exists and it's not loading before assigning the state variable. const MyFuncComponent = () => {
const [notNullData, setNotNullData] = useState();
const { data, error, loading } = useQuery(QUERY);
useEffect(() => {
if (data && !loading) {
setNotNullData(data);
}
}, [data]);
return <ComponentThatRendersData data={notNullData} />;
}; Not sure if there is a better way or not. Also notNullData can actually be null when it's initialized, just bad naming variable on my part. Also here's the updated example: |
@Dastari thanks, but it would really make sense to fix it in this library... I tried to look at the code but couldn't figure out where the problem is. Also, just for completeness, let's use hooks :-P const useQueryNotBugged = (...args) => {
const [notNullData, setNotNullData] = useState();
const { data, error, loading } = useQuery(...args);
useEffect(() => {
if (data && !loading) {
setNotNullData(data);
}
}, [data]);
return { data: notNullData, error, loading };
}; |
I tried to add The issue is the edit: I think I fixed it, I sent a PR (#121) |
@FezVrasta long term it will be solved with a combination of React Suspense and Concurrent Rendering (it should even work in the current version, but it's experimental, and I don't recommend to use it in production). About PR #121 - I don't think it's a good idea - I think it's better to be more explicit than to show stale data. I'm also not sure if it works with fetchMore - please notice that |
@trojanowski react-apollo doesn't empties the data object between refetches, this issue exists exactly because the behavior is different (and generates a bug on our app) |
Please take a look at the same example, but made with |
This is broken at present - upgrading from vanilla React Apollo to this breaks our app because the data disappears as soon as the refresh starts. Added CR comments, please prioritize if possible! |
@trojanowski may we get an updated take on this issue after my recent clarifications? If your concern with my PR is just the If you intend to keep this bug, please at least let us know so we can find alternative solutions. Thank you for your work! |
@FezVrasta I'm not a fan of this behavior (it's a breaking change for me in some cases), but I'm going to fix it to make it similar to react-apollo. I prepared #134 - it uses |
Any news on this ? |
…hat previous value of `data` is shown while loading new one and `networkStatus` should have a correct value in that case BREAKING CHANGE: previous value of `data` is shown while loading new one Closes trojanowski#117 Closes trojanowski#121 Closes trojanowski#129
I created this small example to show my issue:
https://codesandbox.io/s/ovw0m6yl4z
Basically, every time I re-render the component and
useQuery
fetches new data, thedata
object is set to{}
whileloading
istrue
.How can I preserve the previously fetched data while I load new data?
The text was updated successfully, but these errors were encountered: