Skip to content
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

fetching initially false? #245

Closed
tlrobinson opened this issue May 31, 2019 · 8 comments · Fixed by #250 or #256
Closed

fetching initially false? #245

tlrobinson opened this issue May 31, 2019 · 8 comments · Fixed by #250 or #256

Comments

@tlrobinson
Copy link

tlrobinson commented May 31, 2019

All the documentation and code examples suggest if you check that fetching is true and error is falsey then you can assume data is not null.

e.x.

const YourComponent = () => {
  const [{ fetching, data }] = useQuery({ query: `{ todos { id } }` });
  return fetching ? <Loading /> : <List data={data.todos} />;
};

https://github.com/FormidableLabs/urql#quick-start-guide

However, that's not what I'm seeing. fetching is initially false:

Screenshot 2019-05-31 10 43 54

This happens for both the Query component and useQuery hook. Unless you explicitly check that data is not null then these examples will crash.

Or am I missing something?

Version: 1.0.5

@luisrudge
Copy link

I'm having the same issue right now 😬

@d3vhound
Copy link

d3vhound commented Jun 1, 2019

Yeah doesn't really make sense. It's cumbersome to have to work around fetching always being false at initial render. Hope this isn't intentional.

@kitten
Copy link
Member

kitten commented Jun 4, 2019

the PRs #250 and #256 ensure that the initial state you receive from useQuery will always reflect what's going on internally.

They also lay the ground work necessary to implement SSR. So you can expect all of this to ship in v1.1

@larsbs
Copy link

larsbs commented Jun 6, 2019

I still don't understand the reasoning behind this, because now, we can't just simply rely on fetching to know if we should render a loading state or the final component removing the possibility of having one single source of truth.

Also, that always generates an extra rendering cycle for every component. First, fetching = false and data = undefined, and then fetching = true and data = whatever.

@kitten
Copy link
Member

kitten commented Jun 6, 2019

@larsbs as posted above though, this will be fixed in the next version.

@larsbs
Copy link

larsbs commented Jun 6, 2019

@kitten Oh sorry, I understood actually the opposite, that it was expected behavior and it would ship in the final version. Thanks.

@kitten
Copy link
Member

kitten commented Jun 6, 2019

@larsbs sorry, I'll post a more extensive explanation 😅

Basically this was fixed by the two linked PRs which happened to set up some preliminary behaviour for the SSR PR as well.

During initial mount they cause effects and state changes to run synchronously due to two special hooks. Since our exchange pipeline is built to be mostly synchronous this means that the initial state will match the ongoing / completed operation more closely.

When it starts fetching the state change setting it to true will immediately be applied on mount without an additional render.

But additionally this also means that you will immediately get the data you queried if it has already been cached before

@larsbs
Copy link

larsbs commented Jun 6, 2019

That's perfect, so I guess that would also fix the other issue I had #267 good job 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants