question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetching data with hooks

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jamesknelsoncommented, Aug 29, 2019

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.

1reaction
lookfirstcommented, Sep 6, 2019

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:

James would explain it far better than I will, but thanks to packages like isomorphic-unfetch https://www.npmjs.com/package/isomorphic-unfetch you can fetch data and handle errors regardless of the environment, be it a server or a browser.

SSR has its purpose, especially when you care about performance and SEO. You might not need it personally, neither do I at this point, but for bigger applications you probably will have to consider it.

One thing it took me time to realize is that Navi is built with SSR in mind, even-though you can perfectly use it for CSR apps.

It sounds obvious to be me now but until James mentions it, I hadn’t realized getData() main purpose is SSR.

To answer your question, SSR is indeed more complex than CSR but it has become far more friendly for the last couple years.

I actually have big hopes for Navi as it’s got huge potential, knowing that React will eventually release suspense for SSR.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/frontarm/navi/issues/131?email_source=notifications&email_token=AAAU2227PYJCCN2ZRQCSDJDQIHE7RA5CNFSM4IPJBDNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6BTLHQ#issuecomment-528692638, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAU222RFW4BYLZW3AHHAN3QIHE7RANCNFSM4IPJBDNA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch Data from an API - React Hooks Handbook
Learn the basics of asynchronous functions and promises by fetching data from an API using fetch, useEffect and useState.
Read more >
How to fetch data with React Hooks - Robin Wieruch
In order to extract a custom hook for data fetching, move everything that belongs to the data fetching, except for the query state...
Read more >
How To Create A Custom React Hook To Fetch And Cache Data
Throughout this article, we'll be making use of Hacker News Search API to build a custom hook which we can use to fetch...
Read more >
How to fetch data in React using Hooks - Educative.io
Fetching data with a class component · Once the component is mounted, it will fetch data and render it. · An empty dependency...
Read more >
How to fetch data with React Hooks - Mario Kandut
The Effect Hook lets you perform side effects in function components. Data fetching, setting up a subscription, and manually changing the DOM in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found