react icon indicating copy to clipboard operation
react copied to clipboard

Bug: startTransition is causing TypeError when used directly from react

Open raajnadar opened this issue 1 year ago • 3 comments

React version: 18.3.1

Steps To Reproduce

Getting this error message

TypeError: Cannot read property 'add' of undefined

  1. Run the code example https://snack.expo.dev/@raajnadar/swr-error-in-expo-sdk-52
  2. Run android or ios, the web version works without the error
  3. Click the "Call the API button" button inside the mobile app
  4. Check the logs tab, when switched to axios there is no issue
  5. This happens with swr because it uses startTransition internally

Link to code example:

Import like this

export const startTransition: (scope: TransitionFunction) => void =
  IS_REACT_LEGACY
    ? cb => {
        cb()
      }
    : React.startTransition

And use like this

startTransition(() =>
   setState({ data, isMutating: false, error: undefined })
)

Code from this repository https://github.com/vercel/swr/blob/1585a3e37d90ad0df8097b099db38f1afb43c95d/src/mutation/state.ts#L5-L10

The current behavior

Getting this error when startTransition used from React directly

TypeError: Cannot read property 'add' of undefined

On Expo snack the error says

Error: "Cannot read property 'add' of undefined" in TypeError: Cannot read property 'add' of undefined << at requestUpdateLane (/data/user/0/host.exp.exponent/files/.expo-internal/5cb1b0c52b8fcab94364327c83b808ee:18892:43) << at dispatchSetState (/data/user/0/host.exp.exponent/files/.expo-internal/5cb1b0c52b8fcab94364327c83b808ee:16726:33) << at anonymous (swr.mutation:12:14616)

The expected behavior

There is no TypeError,

When I tried using the startTransition from the useTransition hook the problem got solved

raajnadar avatar Nov 23 '24 18:11 raajnadar

This looks like it's also using react-native? Does it repro with react-dom instead of react-native?

I'd inline all the relevant code of swr first. Otherwise it isn't clear for use if the issue is with 3rd party libraries or React itself

eps1lon avatar Nov 24 '24 18:11 eps1lon

With React native web the issue is not visible, we can use the same snack demo to confirm it.

How can I or someone else figure out the issue? I gave my findings here in the issue ticket.

Some devs in an SWR ticket said this should not be an issue from SWR because the latest release was 6 months ago and the same code & SWR version works on expo SDK 51 without change in SWR itself.

Will the problem arise from the React Native core?

raajnadar avatar Nov 25 '24 14:11 raajnadar

I can confirm this issue is not with web apps using react-dom, I will report to react native core

https://stackblitz.com/edit/vitejs-vite-u1tsbe?file=package.json

raajnadar avatar Nov 26 '24 08:11 raajnadar

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

github-actions[bot] avatar Feb 28 '25 07:02 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

github-actions[bot] avatar Mar 07 '25 08:03 github-actions[bot]