Skip to content

Instantly share code, notes, and snippets.

View EthanSK's full-sized avatar
🅱️

Ethan Sarif-Kattan EthanSK

🅱️
  • Cisco
  • London, United Kingdom
View GitHub Profile
@swyxio
swyxio / createCtx-noNullCheck.tsx
Last active May 4, 2023 02:15
better createContext APIs with setters, and no default values, in Typescript. this is documented in https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/blob/master/README.md#context
// create context with no upfront defaultValue
// without having to do undefined check all the time
function createCtx<A>() {
const ctx = React.createContext<A | undefined>(undefined)
function useCtx() {
const c = React.useContext(ctx)
if (!c) throw new Error("useCtx must be inside a Provider with a value")
return c
}
return [useCtx, ctx.Provider] as const
@basarat
basarat / form.tsx
Created October 6, 2016 05:20
React forms that don't reload page
import * as React from 'react';
interface FormProps {
/**
* Automatically prevents the default browser behavior so you don't have to
*/
onSubmit: () => any;
children?: JSX.Element;
}
export const Form = ({ onSubmit, children}: FormProps) =>