TLDR: Sometimes when you design fast, you design wrong. Now, every future change to how my app talks to its API goes to only one module. Before, each component was making those calls and handling errors in their own way.
Yeah, it happened. I was building too fast and too siloed. I’d get excited about one component, build it out and move on to the next. I was so focused on implementing tactically, I didn’t zoom out to remember the strategy. When I came up for air, everything built that needed data from the backend had its own useEffect, its own useState, its own fetch() call, and the pattern repeated across dozens of files.
It wasn’t broken. It was just bad. Turrible…..just turrible.
const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
useEffect(() => {
setLoading(true)
fetch(`${import.meta.env.VITE_SC_DATA_API}/some-endpoint/`)
.then(res => res.json())
.then(d => { setData(d); setLoading(false) })
.catch(err => { setError(err.message); setLoading(false) })
}, [someParam])
That’s what a typical page looked like. Every page had to know the URL env var, handle errors, and race conditions. If you want to change how errors or add a request header, you have to do it in each file. It’s so basic, and yet it happened. I have brought shame to my village. I did learn a new term though. Apparently people refer to this as a ‘shallow’ module problem. You know your boy ain’t shallow. Your boy is deep….so deep….put the code to sleep.
So remembering the words of Dr. Lehr from The Great Texas State University….the rising star of Texas, I separated the concerns. I created a client module that is the seam for all communication to the API. it owns the base URL. It normalizes errors, and exposes a single function:
const BASE = import.meta.env.VITE_SC_DATA_API
async function request(method, path) {
const res = await fetch(`${BASE}${path}`, { method })
if (!res.ok) throw new Error(`${res.status}`)
return res.json()
}
export function get(path) { return request('GET', path) }
That’s the entire file. Now any future file only needs to go there.
To handle the React side for state management, cancellation, and the reset on change, there’s a useQuery:
export function useQuery(path) {
const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
useEffect(() => {
if (!path) { setData(null); setLoading(false); setError(null); return }
let cancelled = false
setLoading(true); setError(null)
get(path)
.then(d => { if (!cancelled) { setData(d); setLoading(false) } })
.catch(e => { if (!cancelled) { setError(e.message); setLoading(false) } })
return () => { cancelled = true }
}, [path])
return { data, loading, error }
}
After the migration, every page that fetches data looks like this:
const { data: ores, loading, error } = useQuery('stuff here')
The component no longer knows about the URL cause it doesn’t need to. Abstraction.
It’s a small refactor, but it really makes it easier to work with.

