Skip to content

Instantly share code, notes, and snippets.

@sumanbh
sumanbh / createCtx-noNullCheck.tsx
Created July 4, 2019 20:17 — forked from swyxio/createCtx-noNullCheck.tsx
better createContext APIs with setters, and no default values, in Typescript
// 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
@sumanbh
sumanbh / postgres_sequence_sync.sql
Last active July 14, 2021 22:10
Postgresql PK sequence out of sync (fix)
--Select the max id(or your primary key)--
SELECT MAX(id) FROM *table*;
--See if the next value in the sequence is bigger than the max id--
SELECT nextval('*table*_id_seq');
--If it isn't make it bigger. This fixes "duplicate key violates unique constraint" error--
SELECT setval('*table*_id_seq', (SELECT MAX(id) FROM *table*)+1);