Write an implementation of an async data call in a useEffect, set loading before and…
- User
Write an implementation of an async data call in a useEffect, set loading before and after the call, and set data depending on if a third variable is true when it returns. Be concise, just the hook
Assistant
import { useState, useEffect } from 'react'; const useAsyncData = (fetchData, shouldSetData) => { const [data, setData] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { const loadData = async () => { setLoading(true); try { const result = await fetchData(); if (shouldSetData) setData(result); } finally { setLoading(false); } }; loadData(); }, [fetchData, shouldSetData]); return { data, loading }; }; export default useAsyncData;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.