Skip to content

Instantly share code, notes, and snippets.

@will-wow
Created January 26, 2020 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-wow/59a721e835fc08bbe58c9596be787fbc to your computer and use it in GitHub Desktop.
Save will-wow/59a721e835fc08bbe58c9596be787fbc to your computer and use it in GitHub Desktop.
RxJS functions with React Hooks
/* eslint-disable react-hooks/exhaustive-deps */
import React from "react";
export const useCombineLatest = <T extends any[], U>(
values: T,
f: (...values: T) => U
): U => {
return React.useMemo(() => f(...values), [f, ...values]);
};
export const useWithLatestFrom = <T, U extends any[], V>(
value: T,
values: U,
f: (value: T, ...values: U) => V
): V => {
return React.useMemo(() => f(value, ...values), [f, value]);
};
export const useMerge = <T>(values: T[]): T => {
const [value, setValue] = React.useState(values[0]);
for (let v of values) {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useEffect(() => setValue(v), [v]);
}
return value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment