Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

All the examples are fetching data from a server, and in such cases I think tanstack query already does all the hard part. I feel like people under-use react query and put too much state in their FE. This might be relevant if your app has some really complicated interactions, but for most apps they should really be a function of server, not client, state. Of course this exact reasoning is why I moved off react altogether and now use htmx in most of my projects
 help



It's not just react query, you can make a quick useFetch and useMutation hooks (or claude can), it's not that complex. If you don't need more advanced features (eg caching), you can easily cut down on 3rd party dependencies.

    import { useState, useEffect } from "react";

    function useFetch(url) {
      const [data, setData] = useState(null);
      const [loading, setLoading] = useState(true);
      const [error, setError] = useState(null);

      useEffect(() => {
        const controller = new AbortController();

        fetch(url, { signal: controller.signal })
          .then((res) => res.json())
          .then((json) => {
            console.log("Data:", json);
            setData(json);
          })
          .catch((err) => {
            if (err.name !== "AbortError") {
              console.error("Fetch error:", err);
              setError(err);
            }
          })
          .finally(() => setLoading(false));

        return () => controller.abort();
      }, [url]);

      return { data, loading, error };
    }







    function App() {
      const { data, loading, error } = useFetch("https://jsonplaceholder.typicode.com/todos/1");

      if (loading) return <p>Loading...</p>;
      if (error) return <p>Error</p>;
      return <pre>{JSON.stringify(data, null, 2)}</pre>;
    }

Yeah, fetching data from a server in useEffect is a widely acknowledged and documented antipattern.

It's a widely documented anti-pattern, while also not giving a convenient alternative.

Is it? I have never had any issues doing it this way.


I am not convinced. Have been using this pattern for years without issue.

You can lead a horse to water...

I have been drinking water for years. You are trying to tell me water is an antipattern and I should drink gatorade instead. No thanks.

I'm doing no such thing. I'm alerting you to the fact that the owner of the well where you've been drinking has warned that it's not potable and suggested drinking the pure, filtered water instead.

Did you even read the article I linked? If not, please do. If you did read it, and still believe it makes no difference, I can't do anything more to help and will simply wish you good luck and have a nice day.


It wasn't the owner that suggested it. It was some random blogplost that insisted the normal town well that everyone used was no good and you have to use his super special well instead. He points to an incident 6 months ago where someone got drunk and decided to jump into the well. The dude who jumped into the well didn't even say it was the well's fault.

I did read the article linked. I am remain unconvinced. Use effect for data fetching is not "risky", "fragile", and does not cause "subtle, hard to debug problems". This is baseless fear mongering. Claiming Tanstack is the "right" way is incredibly arrogant.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: