Next.js/Next.js

refresh()

hihijh826 2024. 2. 15. 16:36
728x90
반응형
SMALL

📍refresh()

- 호출시 현재 경로가 서버에서 업데이트된 할일 목록을 새로고침하고 가져옴

 

☑️예시

- 폼 제출 후 새로고침 하지 않아도 업로드 결과물 보여지는 경우

 

📍사용법

import {useRouter} from 'next/navigation';

const CreatePost = () => {
    const [title , setTitle] = useState("");
    const router = useRouter();
    const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        await fetch('http://127.0.0.1:8090/api/collections/posts/records',{
            method: 'POST',
            headers: { 'Content-Type': 'application/json'},
            body: JSON.stringify({
                title
            })
        })
        setTitle('');
        router.refresh();

    }

 

- import

- router 변수 설정

- router.refresh()

728x90
반응형
LIST