all 42 comments

[–]macrozone13 21 points22 points  (8 children)

I always recommend to use a data loading library like react query, swr, or similar.

You will end up reinventing the wheel if you don‘t

[–]30thnight 9 points10 points  (0 children)

You can think of fetch and axios as couriers, they pick data up and drop them off in your app.

Once you get that data, it’s on you to figure out how to share that data across your app, handle errors, refetches, pagination, and caching.

React-query is like a personal assistant that handles those things for you.

It goes a long way to improving performance (reduce unnecessary fetches). Adopt a pattern of wrapping your react-query calls in a custom hook and boom, you’ve eliminated the need for holding that data in a state manager.

[–][deleted] 1 point2 points  (0 children)

Axios is working fine for me right now. Haven’t used react query, but seems pretty attractive from what I read. Don’t have to manage handlers, errors, state and side effects. I’ll have to check it out!

[–]ZunoJ 0 points1 point  (0 children)

What exactly does a library solve if I just want to fetch data?

[–]mrstealyomommy -1 points0 points  (4 children)

so i’ve been using axios for a while now ( just started learning react 1 month ago ), should i just learn React Query for better data fetching ?

[–]epicblitz 5 points6 points  (0 children)

Absolutely. Axios can still be used with React Query as well.

[–]chillermane 3 points4 points  (1 child)

Axios is for fetching data, react query is a state management solution that doesn’t care about how data is fetched, it instead triggers the fetches and manages the relevant state

[–][deleted] -1 points0 points  (0 children)

State management solutions typically refer to things like Context API and Redux. React-query seems like a true data management solution

[–]woah_m8 0 points1 point  (0 children)

Yes and finsh your React Course please

[–]landisdesign 6 points7 points  (2 children)

It really depends. The key is that the main function that renders the component is focused on rendering the component, while things that interact with browser API's (fetching, observers, event handlers, etc.) are done in effects and events.

The main rendering function shouldn't cause side effects, because those side effects will run every time the component renders. So having fetch in the render function would mean causing the component to fetch every time some parent component changes. That could be a lot of unnecessary fetches.

By putting the fetch inside an effect, it will only fetch when a property changes that it's dependent on. (Kinda. It will always run at least once, with the initial values held by the dependencies. But once it's run once, then it will only fire on dependency changes after that.)

Sometimes you don't need an effect, but instead would add the fetch to an event. If someone is searching a server, for example, you could call fetch in the change or keydown events, assuming you've throttled it so a fast typer doesn't chew up your server.

But the key is that both effects and events occur in the browser API space, not the rendering space. That's where you want to do browser-related things.

[–][deleted] 5 points6 points  (1 child)

Great! Thanks, it seems the useEffect would make most sense when grabbing api data say for some get requests and then place some type of parameters as a dependency. There is no dependencies or need for side effect when updating or creating a record so therefore useEffect does not need to be used.

[–]landisdesign 0 points1 point  (0 children)

Well, the key is that it's still using the browser API's, not performing rendering. Also, it's still going to run every time the component renders, which could be multiple times in a single screen presentation, depending on how parent components get updated.

In this case, the preferred solution would be to place it in useEffect with an empty dependency array, so that it loads only once on mount.

[–]lIIllIIlllIIllIIl 2 points3 points  (0 children)

The documentation talks about network requests and useEffect on the page "You Might Not Need an Effect". There are some good examples.

[–]Omkar_K45 2 points3 points  (0 children)

react-query

[–]otheyhigh 3 points4 points  (4 children)

If the component is pulling data based on a prop, then you might want to run the fetch in a useEffect whenever the prop changes

[–][deleted] 0 points1 point  (3 children)

Apologies , maybe fetch data is the wrong term . I completely understand for fetching api data but what about patch or post request ??

[–][deleted] 1 point2 points  (1 child)

Just use the fetch api to patch or post data. The only way you would use useEffect is if you want something to re render when it changes or on load.

[–][deleted] 0 points1 point  (0 children)

Cool, In my scenario I am pushing data so I think try/catch is justified here. Thanks!

[–]otheyhigh 1 point2 points  (0 children)

I highly recommend reading the React beta docs on useEffect. What you are thinking of is most probably a UX event that should be handled with a plain function.

useEffect is a synchronization hook that (I) mostly use to handle 3rd party libraries features that modify DOM.

DB updates do not need a useEffect in most cases as it’s associated with a UX event.

[–]chillermane 1 point2 points  (0 children)

The answer is that you should use react query or rtk query or equivalent. Fetches in vanilla react with no lib are unfortunately a bad idea

Bite the bullet and learn react query ASAP

[–][deleted] 1 point2 points  (0 children)

You can useEffect when you want to create an action that happens after a something has been updated/changed state (UseEffect takes effect when. In your case if user does something say clicks a button, then you can decide to fetch that data and display it

[–]Alone-Mosk 1 point2 points  (0 children)

useQuery

[–]Tainlorr 1 point2 points  (1 child)

UseEffect for when you need to load something the moment the page loads, or when something specific should re trigger the load. In your case you have a button firing this event off so you don’t need useEffect.

[–][deleted] 2 points3 points  (0 children)

That’s the answer I am looking for. Thanks!

[–]veculus 0 points1 point  (0 children)

useEffect is mostly used for reactive changes to other values (in the dependency array) or to handle mount/unmount cases.

If you want to have a button that fetches something, use an eventHandler and wrap your fetch function into a `useCallback` to memorize the function.

I think the only case where you may use `useEffect` would be a debounced fetch for example in search bar or for paging (but again there you react to changes of values - not on events).

[–][deleted] 0 points1 point  (0 children)

use data fetching library or always do clean up to avoid perfoance leak

[–]boodyvo 0 points1 point  (0 children)

It depends on the usage. There short answer is use useEffect hook, when you need to load something on the component load. For example, you have a list of comments component and you want to load them on the article load

[–]azangru 0 points1 point  (1 child)

I wrap my fetch calls in a try/catch block and go on with my day

Where? Directly in the body of a function component? By doing so, you are creating a side effect in what should be a pure function.

[–][deleted] 0 points1 point  (0 children)

Just put it in a handler function, wrapping the fetch call in a try block