Created
January 26, 2020 04:39
-
-
Save will-wow/59a721e835fc08bbe58c9596be787fbc to your computer and use it in GitHub Desktop.
RxJS functions with React Hooks
This file contains 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
/* 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