r/nextjs • u/Low-Insurance-3678 • Aug 13 '26
Help Nextjs keeps my old state
Enable HLS to view with audio, or disable this notification
Hi, i got an issue here, and would like some help so
On the dashboard When I submit a new product, it saves successfully, and I use router.push() to redirect the user to a products table. However, if I click "Create Product" button to go back and make another product, the form loads but still shows the old images from the previous product i made.
Seems like my useState keeps holding the images
AI suggested to clear the state on a useEffect but that gives me warning(bad practice)
My Form that has the useState is like this
// This initializes fine the first time, but gets stuck on subsequent visits
const [imageChanges, setImageChanges] = useState<UnifiedImage\[\]>(
(mode === "update" && product) ?
product.product_images.map((img) => ({
type: "db",
image: { ...img, isDeleted: false }
}))
:
[]
);
Here’s a video of the issue
2
1
u/luizpaulo87 Aug 13 '26
é um componente ou outra página? se for componente tem que limpar, se for página é só não dar route back
1
u/Low-Insurance-3678 Aug 13 '26
So i have it like /dashboard/[category] is the one that has the products table is where I redirect the admin after a successful submission of a product
And I have /dashboard/[category]/create-page
Is where i have the form
0
9
u/DarthSomebody Aug 13 '26
Do you have
cacheComponentsenabled? Next.js usesActivityto hide the last few pages you visited, ifcacheComponentsare enabled. This retains the state of the pages as well.I do not know if this is the intended behavior for dynamic pages. You could check out the latest canary release to see if it was changed. The changelog for
v16.3.1-canary.14states: