Next.js Discord

Discord Forum

Why doesn't router.refresh() update my dropdown in Next.js 13, while it updates other components?

Unanswered
Miniature Schnauzer posted this in #help-forum
Open in Discord
Miniature SchnauzerOP
Body:

I'm working on a Next.js 13 project where I have two client components: AddFrequentIngreso and AddIngreso. The AddFrequentIngreso component is responsible for adding frequent income sources to a database, and the AddIngreso component has a dropdown that should display these frequent income sources.

// AddFrequentIngreso.tsx
'use client';
// ... (rest of the code)
const handleAddFrequentIngreso = async () => {
  // ... (rest of the code)
  // Emit custom event to notify that a new ingreso has been added
  const event = new CustomEvent('frequentIngresoAdded');
  window.dispatchEvent(event);
  
  router.refresh();
};


// AddIngreso.tsx
'use client';
// ... (rest of the code)
useEffect(() => {
  // ... (rest of the code)
  // Added event listener for custom event 'frequentIngresoAdded'
  window.addEventListener('frequentIngresoAdded', fetchFrequentIngresos);
}, []);


When I add a new frequent income source using AddFrequentIngreso, I want the dropdown in AddIngreso to update automatically without requiring a manual page refresh. I tried using router.refresh() in AddFrequentIngreso after successfully adding a new income source. While this approach updates other components on the page, the dropdown in AddIngreso remains unchanged.

What Each Component Does:

- AddFrequentIngreso: Adds a new frequent income source to the database.
- AddIngreso: Allows the user to add an income entry. It has a dropdown that should list all frequent income sources.

Because router.refresh() didn't work for the dropdown, I resorted to using event listeners as a workaround. However, I'm curious to know why router.refresh() worked for other components but not for this dropdown. Is it because it's a client component? Does Next.js 13 offer a way to update client components specifically?

0 Replies