Three-State Fetching Three-State Data Fetching

Last updated: 2026-11-02

Three-State Fetching Three-State Data Fetching

Every data request has three states: loading while it is in flight, success once the data arrives, and error when it fails. The UI must prepare one frame for each of them, and missing any of them produces a blank screen, an endless wait or a silent failure. This is the mental model shared by hand-written fetch code and by every request library, so learning it once pays off everywhere.

GroupData & Engineering
KindPattern

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

const [state, setState] = useState({ status: 'loading', data: null, error: null })

useEffect(() => {
  fetch(url)
    .then(r => r.ok ? r.json() : Promise.reject(new Error('HTTP ' + r.status)))
    .then(data => setState({ status: 'success', data, error: null }))
    .catch(err => setState({ status: 'error', data: null, error: err }))
}, [url])

if (state.status === 'loading') return <p>Loading…</p>
if (state.status === 'error')   return <p>Error: {state.error.message}</p>
return <List data={state.data} />

⚙️ Parameters

statusThe current state of the machine: 'loading' | 'success' | 'error'; you can also express it with two fields, isLoading and isError.

Returns:No return value; this is a coding pattern rather than a callable API.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

The first automatic request shows "Loading", a successful response reveals the todo title, and when the network or the API fails you get the error message together with a retry button - one frame for each of the three states.

💡 Key Points

⚠️ Warning fetch does not reject on a 404 or a 500 (only network level errors reject), so always check r.ok and throw yourself, otherwise business errors slip silently into the success branch.
💡 Tip Keep the status inside one state object instead of three booleans, which rules out illegal combinations such as "loading is true and error has a value".
ℹ️ Info TanStack Query builds this state machine into the library (isPending, isError, data); writing the three states by hand is the best preparation for it.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Can I use three booleans isLoading/isError/hasData instead of a status?
A You can, but illegal combinations become easy (two of them true at once). A single status field is the state machine way and every combination is valid by construction.
Q Should a failed request retry automatically?
A It depends on the business rules: a read can retry two or three times with an exponential backoff, while a write must be confirmed by a human so that no order is submitted twice.
Q Should the UI be disabled while loading?
A Disable the button of a submit request to prevent double submits; a read does not need to lock the whole screen, a skeleton screen keeps the page interactive.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏