Next.js Discord

Discord Forum

Server Actions - Global State

Answered
Knopper gall posted this in #help-forum
Open in Discord
Knopper gallOP
Is there a way for my server actions to remember a state (without the use of a database)

The idea is that I have a server action that will start or stop a process, which works, but when you reload the page, it 'forgets' that the process is already running.

"use server";

export const HandleStartStopButton = async (isRunning: boolean) => await (isRunning ? Stop() : Start());

let process: NodeJS.Timeout | null = null;

async function Start() {
    if (process) return true;

    process = setInterval(() => {
        console.log("Hello, world!");
    }, 1000);

    return true;
}

async function Stop() {
    if (!process) return false;

    clearInterval(process);
    process = null;

    return false;
}


Hope this makes sense
Answered by Knopper gall
nevermind, it worked, it just required a hard reload
View full answer

1 Reply

Knopper gallOP
nevermind, it worked, it just required a hard reload
Answer