swr: useSWRMutation is not working with React Native New Architecture.

Bug report

Repro Steps / Code Example

  1. npx create-expo-app@latest

configure app.json

 "plugins": [
      "expo-router",
      [
        "expo-build-properties",
        {
          "ios": {
            "newArchEnabled": true
          },
          "android": {
            "newArchEnabled": true
          }
        }
      ]
    ],
  1. home.tsx code example
import useSWRMutation from 'swr/mutation'

function Home () {
  const {trigger, data, error} = useSWRMutation('https://github.com', (key) => fetch(key).then(res => res.text()))

  console.log({data, error}) // always print {"data": undefined, "error": undefined}

  function submit () {
    // print error: "(NOBRIDGE) ERROR  [TypeError: Cannot read property 'add' of undefined]"
    trigger().then(console.log).catch(console.error)
  }

  return  (
     <View>
       <Button title='mutation' onPress={submit}></Button>
     </View>
     )
}

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 13
  • Comments: 26

Most upvoted comments

You can use this patch this works for me

diff --git a/node_modules/swr/dist/mutation/index.js b/node_modules/swr/dist/mutation/index.js
index 0e34690..8607e14 100644
--- a/node_modules/swr/dist/mutation/index.js
+++ b/node_modules/swr/dist/mutation/index.js
@@ -542,9 +542,9 @@ _internal.OBJECT.defineProperty(_internal.SWRConfig, 'defaultValue', {
  * ```
  */ const useSWR = _internal.withArgs(useSWRHandler);
 
-const startTransition = _internal.IS_REACT_LEGACY ? (cb)=>{
-    cb();
-} : ReactExports__default.default.startTransition;
+// const startTransition = _internal.IS_REACT_LEGACY ? (cb)=>{
+//     cb();
+// } : ReactExports__default.default.startTransition;
 /**
  * An implementation of state with dependency-tracking.
  */ const useStateWithDeps = (state)=>{
@@ -610,6 +610,7 @@ const startTransition = _internal.IS_REACT_LEGACY ? (cb)=>{
 };
 
 const mutation = ()=>(key, fetcher, config = {})=>{
+        const [, startTransition] = ReactExports.useTransition()
         const { mutate } = _internal.useSWRConfig();
         const keyRef = ReactExports.useRef(key);
         const fetcherRef = ReactExports.useRef(fetcher);

So we can surely say the issue is with the new architecture and also it is in core react native, there is no problem in SWR library.

Please share any feedback you have in the React native thread, I created a thread to follow up and escalate the issue.

https://github.com/facebook/react-native/issues/47947

I’ve facing the same issue whenever using the mutation. Probably related to the usage of transition.

image

This issue is brutal! Please fix @team

The patch also worked for me, just added in node_modules/swr/dist/mutation/index.js

//find this line const mutation = ()=>(key, fetcher, config = {})=>{ //and added below const [, startTransition] = React.useTransition();

patch doesn’t work well. I’ll switch to react-query

I have the same error and this fix the error: https://github.com/vercel/swr/issues/2986#issuecomment-2561904700

Thank you and I hope the swr team can do something about it.

The patch also worked for me, just added in node_modules/swr/dist/mutation/index.js

//find this line const mutation = ()=>(key, fetcher, config = {})=>{ //and added below const [, startTransition] = React.useTransition();

I don’t understand the cause of the issue and this implementation doesn’t have any problem on other environments, why doesn’t this work on the new architecture?

Seems like it works in new arch without data and isMutating

This works: const { trigger } = useSWRMutation(... but this doesnt const { trigger, data } = useSWRMutation...

So my solution was using useSWRMutation like this:

const [data, setData] = useState(null);
const { trigger } = useSWRMutation('/api/user', updateUser);

useEffect(()=>{
 trigger().then(res=>setData(res))
}, [])

@raajnadar . Yes, I didn’t change any code from Expo Go to Development Build.

The below is my steps:

  1. Build: eas build --profile development --platform ios --local
  2. Run: npx expo start --dev-client

My eas.json:

{
  "build": {
    "development": {
      "developmentClient": true,
      "distribution": "internal",
      "android": {
        "gradleCommand": ":app:assembleDebug"
      },
      "ios": {
        "buildConfiguration": "Debug"
      }
    },
  },
}