Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Router.refresh() not refreshing in next 13?

'use client';

import { useRouter } from "next/navigation";

export default function Todo({ todo }) {
    const router = useRouter();
    return (
        <>
            <li key={todo.id}>
                <input type='checkbox' checked={todo.isDone} onChange={(e) => update(todo.id, e.target.checked, router.refresh)}></input>
                {todo.name}
                <button onClick={() => todoDelete(todo.id, router)}>Delete</button>
            </li>
        </>
    )

    async function todoDelete(id, router) {
        await fetch(`/api/todo/delete?id=${id}`, {
            method: 'DELETE',
    
        });
        router.refresh();
    }
    
}

The code seems to jump right over the refresh() function, but the rest of the API call is working fine. Anyone know why I'm unable to refresh?

like image 429
Typhon Avatar asked Dec 02 '25 08:12

Typhon


2 Answers

Also as solution is using native reload page from JS

// Refresh the page
location.reload();
like image 183
Mykola Berezhniuk Avatar answered Dec 03 '25 20:12

Mykola Berezhniuk


I was having a similar issue. In my case, I did router.refresh(); first followed by router.push('/users') to navigate back to the data table from the update page.

However, the correct implementation would be to do router.push('/users') first, followed by router.refresh();.

like image 39
ʜᴛᴋ Avatar answered Dec 03 '25 21:12

ʜᴛᴋ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!