Skip to content

Instantly share code, notes, and snippets.

View stephanedupont's full-sized avatar

Stéphane Dupont stephanedupont

View GitHub Profile
@stephanedupont
stephanedupont / useHistory.ts
Created January 21, 2023 11:51
History React Hook
import React, {useCallback} from "react";
export function useHistory<T>(state: T): [
pushState: (state: T) => void,
undo: () => T | null,
redo: () => T | null,
] {
const [history] = React.useState<{ data: Array<T>, index: number }>({data: [state], index: 1});