Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Created December 5, 2020 16:10
Show Gist options
  • Save fauxsaurus/c0314b3b498c6696017981e426a7232b to your computer and use it in GitHub Desktop.
Save fauxsaurus/c0314b3b498c6696017981e426a7232b to your computer and use it in GitHub Desktop.
import {useState} from 'react';
export const useVector = <T>(initialValue: T, arity = 3) => {
const [vector, setVector] = useState<T[]>([...Array(arity)].fill(initialValue));
const updateVector = (newValue: T) => setVector([newValue].concat(vector.slice(1)));
return [vector, updateVector] as const;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment