NextJS State Props not Updating on NGINX droplet
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
I have a NextJS app with dynamic pages that grab from a database. For one file, it uses the useEffect hook and grabs a unique page by id in the form of an object and perfectly works. However, when it comes to things like findMany, where I am grabbing an array of objects, the states dont update at all but rather stay the same until I run 'npm run build' again.
How do I fix this?
//returns non-array (working)
useEffect(() => {
const fetchMember = async () => {
await getMemberById(Number(id))
.then(res => {
if(res)
setMemberData(res)
else
router.push('/')
})
}
fetchMember()
}, [])
return (
<AdminPageWrapper title={memberData ? memberData.first_name + " " + memberData.last_name : "Loading..."} redirect="/admin/members">
<AdminMemberDetails memberData={memberData ? memberData : {} as Prisma.PromiseReturnType<typeof getMemberById>}></AdminMemberDetails>
</AdminPageWrapper>
)
//returns array (not working)
useEffect(() => {
const fetchEvents = async () => {
await getEvents()
.then(res => {
setEventData(res)
})
}
fetchEvents()
}, [])
return (
<AdminPageWrapper title="Events" redirect="/admin">
<AdminEventsList eventsData={eventData} />
</AdminPageWrapper>
)How do I fix this?
7 Replies
Japanese flying squid
Have you tried setEventData([...res]) ?
Also, maybe this information can be helpful:
If you use Axios:
The fetched data will remain static and won't update dynamically. To see changes in the fetched data, you'll need to rebuild the application.
If you use Fetch with cache set to "no-cache":
The fetched data will update dynamically as it changes.
If you use Axios:
The fetched data will remain static and won't update dynamically. To see changes in the fetched data, you'll need to rebuild the application.
If you use Fetch with cache set to "no-cache":
The fetched data will update dynamically as it changes.
@Japanese flying squid Have you tried setEventData([...res]) ?
Japanese flying squid
Try this on the first place
Spectacled bearOP
Okay I will try in the morning
I’ll see how it goes
Thanks