New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing check for promise function together with the inputs #8
Comments
I think we can't use
One solution is designing const usePromise = (
promise: (...inputs: any) => any,
inputs: Array<any>,
category: string = promise.toString(),
lifespan: number = 0
)
|
For now, I'm using this wrapper in my projects: import originalUsePromise from 'react-promise-suspense'
type Options = {
category?: string
lifespan?: number
}
export default function usePromise<T>(
promise: (...inputs: any[]) => Promise<T>,
inputs: any[],
options: Options = {}
): T {
const category = options.category || promise.toString()
const lifespan = options.lifespan || 0
const cacheKeys = [category, ...inputs]
const callPromise = () => promise.apply(null, inputs)
return originalUsePromise(callPromise, cacheKeys, lifespan)
} |
@ngocdaothanh Thanks for giving your snippet, it is really insightful. You are right we should not compare the function reference (I was proposing that because at that time most of my functions were declared outside of render function and thus they were static) However this library is not in maintain and has other issues as well which the author doesnt seem to have interest on. So, do you think that you would like to fork the repo and publish a new package? I'm glad to support and use it in my projects. |
@namnm From the issues you created at this repo, it seems you are very passionate about this project, and you have more than a year of experience with this project, more than me. So please fork the repo and publish a new package. |
You can use fork of this package that solves this issues and many others react-use-await |
We need to improve the if statement in the cache check to:
if (promiseCache.promiseFn === promise && deepEqual(inputs, promiseCache.inputs))
Example of an invalid case:
To do so, when construct the cache item, you need to save the promiseFn as well:
The text was updated successfully, but these errors were encountered: