signals icon indicating copy to clipboard operation
signals copied to clipboard

New version of preact signals for react not working with React Native

Open XantreDev opened this issue 2 years ago • 22 comments

In my RN apps, preact signals not working. Previous implementation wasn't working because of navigation.

 ERROR  TypeError: Cannot read property 'alternate' of null

This error is located at:
    in NativeStackNavigator (created by App)
    in EnsureSingleNavigator
    in BaseNavigationContainer
    in ThemeProvider
    in NavigationContainerInner (created by App)
    in QueryClientProvider (created by PersistQueryClientProvider)
    in PersistQueryClientProvider (created by CacheProvider)
    in CacheProvider (created by App)
    in App (created by gestureHandlerRootHOC(App))
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by gestureHandlerRootHOC(App))
    in gestureHandlerRootHOC(App)
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in oone_drive(RootComponent), js engine: hermes

However its working in expo snack: https://snack.expo.dev/@xantregodlike/funny-pretzel?platform=android This issue is not reproducing event in pure react native starter. I think it can be related with some external libraries. When i will detect which one is causing this issue - it will add reproduce steps

XantreDev avatar Apr 13 '23 15:04 XantreDev

@andrewiggins

XantreDev avatar Apr 13 '23 16:04 XantreDev

Signals is working in pure repo. So we will investigate more about it

XantreDev avatar Apr 14 '23 08:04 XantreDev

I'm having the same issue

fadi-quader-mox avatar Apr 14 '23 10:04 fadi-quader-mox

The solution for it is to roll back to 1.2.1 and apply a little patch (if you are using react-navigation) #257

Voznov avatar Apr 14 '23 13:04 Voznov

same here

leobastiani avatar Apr 15 '23 00:04 leobastiani

I get the same error in a plain React app with vite -m production build. Works fine with vite. Here's the console errors... image

pmartin-cogility avatar May 04 '23 20:05 pmartin-cogility

Same for me with fresh Expo project, signals v1.2.2 works though

geakstr avatar Jun 17 '23 17:06 geakstr

I'm checking diff between 1.2.2 and 1.3.0 and... I'm not exactly sure yet but it might be related that signals >=v1.3.0 started to use __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED from react package

https://github.com/preactjs/signals/compare/%40preact/signals-react%401.2.2...%40preact/signals-react%401.3.0

geakstr avatar Jun 17 '23 17:06 geakstr

Hint, hint? __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED Fired!

pmartin-cogility avatar Jun 18 '23 02:06 pmartin-cogility

Seems to be one of the reason - wrong function serialization

 LOG  useCallbackImpl function throwInvalidHookError() { [bytecode] }
 LOG  useCallbackImpl function useCallback(a0, a1) { [bytecode] }
 LOG  useCallbackImpl function useCallback(a0, a1) { [bytecode] }
 LOG  useCallbackImpl function useCallback(a0, a1) { [bytecode] }
 LOG  function bebe() { [bytecode] }
 LOG  useCallbackImpl function useCallback(a0, a1) { [bytecode] }

XantreDev avatar Jun 22 '23 03:06 XantreDev

This is not bug - this the feature https://github.com/facebook/hermes/issues/114

They are added 'show source' to actually check function code

XantreDev avatar Jun 22 '23 04:06 XantreDev

We actually can actually patch react-native, but there are many renderers (react-native-skia, three-fiber). I think its blocker in terms of React Native support.

Can we give user opportunity to select auto tracking method, because old auto tracking via wrapping each component with proxy working very well in case of react native. @andrewiggins What do you think?

Maybe it should be @preact/signals-react-native package, or just "react-native" field in package.json exports

XantreDev avatar Jun 22 '23 22:06 XantreDev

I'm currently pursuing a method that would use a babel transform on components to track signals instead of patching internals. That should hopefully work better for all renderers. PR for feedback should be out soon (a couple days).

andrewiggins avatar Jun 22 '23 22:06 andrewiggins

It would be really interesting. Appreciate your work

XantreDev avatar Jun 23 '23 10:06 XantreDev

@andrewiggins Can i create PR to add information for docs how to use preact signals in react native?

XantreDev avatar Jun 24 '23 09:06 XantreDev

I couldn't even get v1.2.0 to work with my Expo setup, so I ended up just going with Jotai just to get something that works right now: https://jotai.org/docs/guides/react-native

It's not ideal and not as clean, but it works for now, especially if it's a new project and you can't wait for an official patch.

ThatGuySam avatar Jul 23 '23 18:07 ThatGuySam

@andrewiggins when real way to use babel transform will be provided?

XantreDev avatar Aug 08 '23 14:08 XantreDev

Initial version of babel transform is available at https://npm.im/@preact/signals-react-transform Give it a try and lemme know how it goes.

andrewiggins avatar Aug 09 '23 13:08 andrewiggins

But how to use it?). We should install @preact/signals-core and write useSignal, useComputed..., manually? If we will use @preact/signals-react it will patch react and broke the app. In case of pnpm we can produce preact signals runtime duplication, because we should install @preact/signals-react and @preact/signals-react-transform will have its own version of @preact/signals-react

XantreDev avatar Aug 09 '23 14:08 XantreDev

The documentation for @preact/signals-react-transform implies that it's not possible to opt into the "bypass VDOM" optimization. Yes?

Perhaps a step back to a simplified approach where there's no patching React? Instead, provide a simple component that can do the optimization? <Signal $={mySignal} />? Add yet another library: @preact/signals-react-plain?

pmartin-cogility avatar Aug 11 '23 17:08 pmartin-cogility

Perhaps a step back to a simplified approach where there's no patching React? Instead, provide a simple component that can do the optimization? <Signal $={mySignal} />?

How would this work? The issue resides in React's reconciler, any special component you make is subject to the reconciler just as every other component is.

rschristian avatar Aug 18 '23 06:08 rschristian

@andrewiggins what do you think about my concerns?

XantreDev avatar Sep 02 '23 16:09 XantreDev