swr: useSWRMutation is not working with React Native New Architecture.
Bug report
Repro Steps / Code Example
- npx create-expo-app@latest
configure app.json
"plugins": [
"expo-router",
[
"expo-build-properties",
{
"ios": {
"newArchEnabled": true
},
"android": {
"newArchEnabled": true
}
}
]
],
- 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
You can use this patch this works for me
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.
This issue is brutal! Please fix @team
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
dataandisMutatingThis works:
const { trigger } = useSWRMutation(...but this doesntconst { trigger, data } = useSWRMutation...So my solution was using
useSWRMutationlike this:@raajnadar . Yes, I didn’t change any code from Expo Go to Development Build.
The below is my steps:
eas build --profile development --platform ios --localnpx expo start --dev-clientMy
eas.json:Found this ussue ticket - https://github.com/facebook/react-native/issues/47730#issuecomment-2495544910