I'm working on learning NextJS. There might be an easy fix I'm not seeing so I appreciate if you all would take a look.
I have an API queryData(id)
which will return data based on id
.
profile
will contain the user's profile which is a list of IDs.
subs.js
export default function Subs() {
const { profile } = useProfileContext()
const [userSubs, setUserSubs] = useState()
useEffect(() => {
const subArray = []
profile.subIdList.map(subId => {
console.log("ID: ", subId)
queryData(subId.trim()).then(data => {
subArray.push(data)
return data
})
})
console.log("USE EFFECT: ", subArray) // Everything is as expected: List of JSON objects with metadata corresponding to ID.
setUserSubs(subArray)
}, [])
return (
<>
top
{
userSubs?.map(x => {
return <p>hello {x}</p> // Same result with or without return
})
}
end
</>
)
}
I expect to see something like:
top
hello data1
hello data2
hello data3
end
But the actual result is:
topend
So it's clear the data from the API is successful in being written into the state but the contents are not rendering properly from what I suspect the the map function isn't being rendered again after the userState is updated.
I've tried with replacing the default values for state with [1, 2, 3] and the setState([4, 5, 6]) and the render will be 4, 5, 6 so the issue is not the structure of the states but this does use return <p>...</p>
in the map.