Fetching data with hooks
See original GitHub issueI would like to fetch remote data in getData
using react hooks, however this can’t be done within regular javascript functions as per React rules of hooks.
getData
being a regular function that returns object | Promise<object>
, hooks can’t be used there.
My concern is that more and more libraries are embracing hooks for data fetching, (e.g React Apollo hooks).
I could obviously use hooks in my pages/components, but that would kind of defeat the purpose of react-navi getData
.
Do you have any advice on what would be the best approach for this issue?
Edit: I found a somewhat similar issue there.
I could use plain promise based queries using apollo client as per the above issue example, but I would still really like to use hooks instead as I can auto generate them thanks to a package called graphql-gen, which makes it far easier and less error prone compared to using the bare apollo client.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
The issue with hooks is that
useEffect()
is not designed to be run on the server, so for SSR, you need a non-React way to fetch your initial data.One option would be to provide a hooks-like API within Navi, but that’s… a lot of work and maintenance, and will never be quite as useful as just being able to use hooks.
This is a tricky problem. I’ll leave the issue open, but I really don’t know how to approach this right now.
Oh, don’t get me wrong. Navi is my favorite router ever. Extremely well done.
On Fri, Sep 6, 2019 at 10:28 AM Pierre Lebrun notifications@github.com wrote: