Skip to content

cannot use cache provider in React.StrictMode #1537

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

Closed
bash-wealthkernel opened this issue Oct 8, 2021 · 3 comments · Fixed by #1539
Closed

cannot use cache provider in React.StrictMode #1537

bash-wealthkernel opened this issue Oct 8, 2021 · 3 comments · Fixed by #1539

Comments

@bash-wealthkernel
Copy link

Bug report

Description / Observed Behavior

When using SWRConfig to set the cache provider it is ignored when used in React.StrictMode. I believe this has to do with StrictMode rendering function components twice (docs). When initCache is called the second time around with the same cache it returns undefined and therefore the provider never ends up using the value (here).

Expected Behavior

The provided configuration should be respected.

Repro Steps / Code Example

https://codesandbox.io/s/awesome-moon-4d8xb
Toggle strict to trigger the issue.

Additional Context

"react": "17.0.2",
"react-dom": "17.0.2",
"react-scripts": "4.0.3",
"swr": "1.0.1",
@huozhi huozhi added the good first issue Good for newcomers label Oct 8, 2021
@shuding
Copy link
Member

shuding commented Oct 8, 2021

You don’t need to use an extra useState to initialize the cache, just use:

provider: () => new Map()

It already has state reusing inside.

@shuding shuding closed this as completed Oct 8, 2021
@shuding shuding removed the good first issue Good for newcomers label Oct 8, 2021
@shuding
Copy link
Member

shuding commented Oct 8, 2021

(Removed “good first issue” as well since I don’t think strict mode and lazy state initialization are friendly for new contributors cc @huozhi)

@shuding
Copy link
Member

shuding commented Oct 8, 2021

I noticed that there's still a bug here, opening a PR to address.

@huozhi huozhi linked a pull request Oct 9, 2021 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants