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

React 18 in concurrent mode + Cache provider #1904

Closed
piotr-cz opened this issue Apr 5, 2022 · 8 comments
Closed

React 18 in concurrent mode + Cache provider #1904

piotr-cz opened this issue Apr 5, 2022 · 8 comments
Labels
area: concurrent bug Something isn't working
Milestone

Comments

@piotr-cz
Copy link

piotr-cz commented Apr 5, 2022

Bug report

Description / Observed Behavior

When using React 18 in concurrent mode and a custom cache provider, an error is thrown:

Uncaught TypeError: Cannot read properties of undefined (reading '0')
    at useSWRHandler (index.esm.js:631:1)

This happens when using create-react-app v4 or v5 in dev mode (react-scripts start), however not when running an actual build (react-scripts build).

Expected Behavior

No error is thrown

Repro Steps / Code Example

CodeSandbox based on Basic usage is available here: https://codesandbox.io/s/eager-alex-zyijly?file=/src/index.js:

  1. Update react and react-dom to 18.0 or 18.1 in dependencies, enable concurrent mode (ReactDOM.createRoot(rootElement).render(...))
  2. Add new provider const provider = () => new Map();
  3. Wrap <App /> with <SWRConfig value={{ provider }}></SWRConfig>

Additional Context

SWR version: 1.1.0 ... 1.3.0 || 2.0.0-beta.0 || 2.0.0-beta.1
Latest version with no error: 1.0.1

It looks like the SWRGlobalState WeakMap in the useSWRHandler function doesn't contain custom cache provider, even that it has been successfully added in initCache function.

@emreycolakoglu
Copy link

emreycolakoglu commented Apr 21, 2022

Hey, this breaks us pretty bad too. Had to create a workaround using middlewares for my use-case (persistance) but it's far from perfect.
Is there a workaround?
NextJS 12.1.5
Swr 1.3.0
React 18.0.0

@piotr-cz
Copy link
Author

Any workarounds I know of is downgrading react to v17 or swr to v1.1.
I guess this issue will receive more attention when people will start upgrading react to v18.

@shuding shuding added bug Something isn't working area: concurrent labels Apr 21, 2022
@shuding shuding added this to the 2.0 milestone Apr 21, 2022
@shuding
Copy link
Member

shuding commented Apr 21, 2022

React 18 support will be our top priority for 2.0 and it’s in the works.

@promer94
Copy link
Collaborator

This should works in latest beta version now.
codesandbox

@piotr-cz
Copy link
Author

piotr-cz commented May 16, 2022

@promer94
I cannot completely confirm, as upgrading swr in my sandbox to 2.0.0-beta.3 as well as downgrading in yours leads to

ModuleNotFoundError
Could not find module in path: 'swr' relative to '/src/index.js'

I have no idea what's going on

@promer94
Copy link
Collaborator

@piotr-cz could you try to refresh the page. I had same problem but works after reloading page. I think it might be some cache problem of codesandbox.

@piotr-cz
Copy link
Author

@promer94
Thanks, it works!

right click into result page -> Reload Frame

@shuding
Copy link
Member

shuding commented May 16, 2022

Great! I guess we can close this issue now? 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: concurrent bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants