Created
July 12, 2021 11:54
-
-
Save codemile/fb91e9b74e05f7c0c94f208d955e1278 to your computer and use it in GitHub Desktop.
ReactJs hook that joins an Array of Elements with a separating Element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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