Skip to content

Instantly share code, notes, and snippets.

@Eleven-am
Eleven-am / useServerAction.ts
Last active May 18, 2023 18:16
This hook allows you to use the new server actions introduced in next's 13.4.
'use client';
import { useState, useRef, useMemo, useTransition, useCallback, useEffect } from 'react';
export type ServerAction = (...args: any[]) => Promise<any>;
export type Input<T extends ServerAction> = Parameters<T>;
export type Output<T extends ServerAction> = ReturnType<T> extends Promise<infer R> ? R : never;
type ServerActionType<T extends ServerAction, Value> = {
data: Value;
@Eleven-am
Eleven-am / phœnix-channels-react-hook.tsx
Last active August 15, 2022 09:39
This is a relatively typed phoenix channels hook that works incredibly well with react. you can create the same channels in multiple components as they share the same channel singleton
import {
createContext,
ReactNode,
useCallback,
useContext,
useEffect, useMemo,
useRef,
useState
} from "react";
import {Channel, Presence, Socket} from "phoenix";