Skip to content

Instantly share code, notes, and snippets.

@codemile
Created July 12, 2021 11:54
Show Gist options
  • Save codemile/fb91e9b74e05f7c0c94f208d955e1278 to your computer and use it in GitHub Desktop.
Save codemile/fb91e9b74e05f7c0c94f208d955e1278 to your computer and use it in GitHub Desktop.
ReactJs hook that joins an Array of Elements with a separating Element.
export const useArrayJoin = (
arr: Array<JSX.Element>,
join: (key: number) => JSX.Element
) => {
return useMemo(() => {
return arr.reduce(
(acc: null | Array<JSX.Element>, next: JSX.Element, key) =>
acc === null ? [next] : [...acc, join(key), next],
null
);
}, [arr, join]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment