Skip to content
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

useResource 支持配置cacheOptions #31

Open
jacketwpbb opened this issue Nov 2, 2023 · 7 comments
Open

useResource 支持配置cacheOptions #31

jacketwpbb opened this issue Nov 2, 2023 · 7 comments

Comments

@jacketwpbb
Copy link

当请求失败时不cache结果

@otakustay
Copy link
Member

那如果这个请求“注定”失败,就会无限请求哦,一瞬间就会刷出成千上万个HTTP请求来……

@otakustay
Copy link
Member

如果在一个场景下,重试能解决问题,建议直接在请求函数上包一下重试的逻辑就行了,也能自定义重试之间等一段时间之类的。放到组件这一层不做缓存,无限刷请求太容易出问题了

重试甚至可以用类似p-retry这样的包解决,一行代码就行

@jacketwpbb
Copy link
Author

那如果这个请求“注定”失败,就会无限请求哦,一瞬间就会刷出成千上万个HTTP请求来……

失败了不就走renderError了吗, 目前业务场景是出错了 renderError里渲染了一个返回按钮history.navagateBack, 但是下次进来的时候希望可以重新请求,而不是用缓存结果 renderError

@otakustay
Copy link
Member

renderError的前提就是异常被缓存着,然后在render函数中被throw出来。这是SuspenseErrorBoundary必须的逻辑,并不是你想象中的类似useState的那套逻辑。在Suspense这套中,出错了自然会重新render一下,这时就已经是“下次进来”了。你要“下次进来重新请求”,那就是死循环重新请求

@otakustay
Copy link
Member

有一种绕过去的办法,就是在renderError里,渲染一个组件,用这个组件的useEffect来清掉缓存。前提是你知道出错的请求函数和参数是什么,不然也找不到缓存在哪里

又或者,在renderError上可以把函数和参数也给过来……

@jacketwpbb
Copy link
Author

jacketwpbb commented Nov 2, 2023

RenderErrorOptions 多传一个expire可行吗

    private renderError(error: Error) {
        const {renderError, onExpireResource} = this.props;
        const recover = () => {
            if (error instanceof SuspenseError) {
                onExpireResource?.(error.action, error.params);
            }
            this.setState({error: null});
        };
        return renderError(error instanceof SuspenseError ? error.cause : error, {recover});
    }

@otakustay
Copy link
Member

我要做个实验,expire调用后是会刷组件的,但我不确定已经Error时会不会刷掉,会的话又死循环了……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants