5. You want to fetch user data only once when a component mounts and store it in state. Which code correctly implements this mounting phase behavior?
function UserProfile() {
const [user, setUser] = React.useState(null);
// Which useEffect is correct?
A) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, []);
B) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
});
C) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, [user]);
D) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, [setUser]);
return user ? <div>{user.name}</div> : <div>Loading...</div>;
}