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

Conditionally firing a query #21

Closed
shlomokraus opened this issue Nov 21, 2018 · 2 comments
Closed

Conditionally firing a query #21

shlomokraus opened this issue Nov 21, 2018 · 2 comments

Comments

@shlomokraus
Copy link

I am trying to implement a conditional pattern which I am not sure how it should work with hooks.
I have one query asking for currentUser and only when and if it returns a userId I will execute the query.

By the rules of hooks, you must not nest hooks inside conditions or loops, so how can I achieve this?

This is a sample of my custom hook:

function useSomething() { 
  const [userId] = useCurrentUser();
  // Here we need to skip the query
  const { data, errors } = useQuery(query, { variables: {userId} });
  // Do some stuff and return data
 return [data, errors]
}
@trojanowski
Copy link
Owner

Hi @shlomokraus

Right now the only way to do it is to conditionally render a component using the useQuery hook. I think we could add skip option to it. The query wouldn't run if it were equal to true.
Also please look at #18 (comment)

@shlomokraus
Copy link
Author

I also think that adding "skip" or similar is the best solution. Rendering the component conditionally will break encapsulation.

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

No branches or pull requests

2 participants