๐Ÿ“ฆ AlessioGr / root-layout-rerender-repro

๐Ÿ“„ ServerActionProvider.tsx ยท 18 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18'use client'
import React from 'react';

export const ServerActionContext = React.createContext<{
    serverAction: () => void;
} | null>(null);

export const useServerAction = () => {
    const context = React.useContext(ServerActionContext);
    if (!context) {
        throw new Error('useServerAction must be used within a ServerActionProvider');
    }
    return context;
}

export const ServerActionProvider: React.FC<{ serverAction: () => void }> = ({ serverAction, children }) => {
    return <ServerActionContext.Provider value={{ serverAction }}>{children}</ServerActionContext.Provider>
}