StepsEasy
React useEffect - Example
Run
Reset
Destroy & Close
← Back to lesson
Code Editor
javascript
import { useState, useEffect } from 'react'; function UserProfile({ userId }) { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { setLoading(true); fetch(`/api/users/${userId}`) .then(res => res.json()) .then(data => { setUser(data); setLoading(false); }); return () => console.log('Cleanup!'); }, [userId]); if (loading) return <p>Loading...</p>; return <h1>{user.name}</h1>; }
Result
Click "Run" to execute the code...
Loading runtime...
Ready
JAVASCRIPT | In-Browser Execution