useState setter not working in useEffect with async API call
25 replies
Last updated: Jan 28, 2026
AI Update
This is a common React issue, not specifically a Sanity problem, but I can help! The issue is that setSessions is a state setter function, not an async function - it doesn't return a promise, so you shouldn't await it.
Here's the corrected code:
useEffect(() => {
const getSessions = async () => {
const data = await fetch("https://api.call")
const json = await data.json()
setSessions(json.collection) // Remove the await here!
}
getSessions().catch(console.error);
}, [])Why this happens:
setSessionsis a synchronous function that schedules a state update- It doesn't return a promise, so
awaitdoesn't make sense here - React batches state updates and applies them after your function completes
Additional improvements for your code:
- Fix the map syntax error - you have a syntax error in your map function:
{sessions.map((s) => (
<option key={s.id || s.name} value={s.name}>
{s.name}
</option>
))}- Add loading state to handle the async period:
const [sessions, setSessions] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
const getSessions = async () => {
try {
const data = await fetch("https://api.call")
const json = await data.json()
setSessions(json.collection)
} catch (error) {
console.error(error)
} finally {
setLoading(false)
}
}
getSessions()
}, [])- Consider using Sanity's built-in hooks - if you're fetching Sanity data, you might want to use hooks like
useLiveQueryinstead of raw fetch calls for better integration with the Studio.
The main fix is removing await from setSessions - that should get your component working!
Show original thread25 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.