Next.js Discord

Discord Forum

Can't Update the API data dynamically

Unanswered
Elm sawfly posted this in #help-forum
Open in Discord
Elm sawflyOP
    const getPasswords = async () => {
        try {
            const res = await fetch("/api/passwords", {
                next: {
                    revalidate: 3
                }
            });
            const data = await res.json();
            setPasswords(data);
            return data;
        } catch (err) {
            console.log(err);
        };
    };

    const deletePassword = async (id: string) => {
        try {
            const { data } = await axios.delete(`/api/passwords/${id}`);
            router.refresh();
        } catch (err) {
            console.log(err);
        };
    };

    useEffect(() => {
        getPasswords();
    }, []);

16 Replies

@@ts-ignore also try moving data fetching to the server as much as you can
Elm sawflyOP
By making a folder of name helpers and making the event handler there?
@@ts-ignore you will need to manually call the function again
Elm sawflyOP
Where?
In the delete handler?
@Elm sawfly By making a folder of name `helpers` and making the event handler there?
are you fetching the data based on user's interaction?
@@ts-ignore are you fetching the data based on user's interaction?
Elm sawflyOP
No no
The data should be server as the user loads the page
@Elm sawfly No no The data should be server as the user loads the page
yeah then fetch it inside a server component
Elm sawflyOP
ok
thx
@@ts-ignore yeah then fetch it inside a server component
Elm sawflyOP
Just only fetch?
Or make the handler inside the server component?
Sorry if i am bothering too much