ReactJS Program to Abort Fetch On Unmount with Explanation
ReactJS
Hard
Hooks & Effects
29 views
1 min read
86 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around abort, fetch, abortcontroller. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Fetch data and cancel the request using AbortController on cleanup.
Input Format
No input.
Output Format
Render a React component.
Constraints
Create AbortController and call abort in cleanup.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useEffect, useState } from 'react';
export default function App() {
const [status, setStatus] = useState('idle');
const [items, setItems] = useState([]);
useEffect(() => {
const controller = new AbortController();
async function load() {
setStatus('loading');
try {
const res = await fetch('https://jsonplaceholder.typicode.com/users?_limit=5', { signal: controller.signal });
const json = await res.json();
setItems(json);
setStatus('success');
} catch (e) {
if (String(e && e.name) === 'AbortError') return;
setStatus('error');
}
}
load();
return () => controller.abort();
}, []);
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode users</h2>
{status === 'loading' ? <div>Loading...</div> : null}
{status === 'error' ? <div style={{ color: '#c1121f' }}>Request failed</div> : null}
{status === 'success' ? (
<ul style={{ paddingLeft: 18 }}>
{items.map((u) => <li key={u.id}>{u.name}</li>)}
</ul>
) : null}
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Fetch data and cancel the request using AbortController on cleanup.
Input / Output
Output
Render a React component.
Constraints
Create AbortController and call abort in cleanup.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
import React, { useEffect, useState } from 'react';
export default function App() {
const [status, setStatus] = useState('idle');
const [items, setItems] = useState([]);
useEffect(() => {
const controller = new AbortController();
async function load() {
setStatus('loading');
try {
const res = await fetch('https://jsonplaceholder.typicode.com/users?_limit=5', { signal: controller.signal });
const json = await res.json();
setItems(json);
setStatus('success');
} catch (e) {
if (String(e && e.name) === 'AbortError') return;
setStatus('error');
}
}
load();
return () => controller.abort();
}, []);
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode users</h2>
{status === 'loading' ? <div>Loading...</div> : null}
{status === 'error' ? <div style={{ color: '#c1121f' }}>Request failed</div> : null}
{status === 'success' ? (
<ul style={{ paddingLeft: 18 }}>
{items.map((u) => <li key={u.id}>{u.name}</li>)}
</ul>
) : null}
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!