signals icon indicating copy to clipboard operation
signals copied to clipboard

Using signals package with react router results in a wierd jsx error

Open salamaashoush opened this issue 2 years ago • 2 comments

I tried using @preact/signals-react in a project with react-router 6 but I got that error which doesn't make any sense to me, the error is complaining about react-router but originates from @preact/signals-react and only happens when I import/use anything from @preact/signals-react

image

here is a link https://stackblitz.com/edit/react-router-starter-gfxkdm?file=components%2FHome.js to reproduce the error, try commenting out the signal import/usage in Home.js and everything will work fine

I think it could be related to the patches to JSX factories, which breaks the react-router check for the <Route/> element type somehow, if I used it with the routing config approach from react-router 6.4 it works fine

salamaashoush avatar Oct 24 '22 20:10 salamaashoush

couldn't find a workaround for this error so I had to use the @preact/signals-core package with this custom hook

export function useSignalValue<T>(signal: Signal<T>) {
  const [value, setValue] = useState<T>(() => signal.peek());
  useEffect(() => signal.subscribe(setValue), [signal]);
  return value;
}

that allowed me to update the signals from anywhere and then where I use that hook react will rerender because of the local state update

it is not the best/optimal solution but hopefully, this issue with react-router could be fixed

salamaashoush avatar Oct 26 '22 12:10 salamaashoush

I have the same error, using @react-navigation/native (6.0.13) and @react-navigation/stacks (6.3.2). Signals seems to insert itself into the list of children of the Stack.Navigator component, causing it to trigger a runtime exception. Signals thus cannot presently be used in RN apps that rely on react-navigation.

vthorsteinsson avatar Oct 26 '22 22:10 vthorsteinsson

Ran into this today. We may need to consider finding a way to use SECRET_INTERNALS again.

developit avatar Nov 17 '22 16:11 developit

Problem is the proxy around every React element type (the actual components) to enable rerending when signals change. So the proxy around the Route component breaks the element.type === Route check inside react-router. @developit Interested in how you plan to fix this. Strict equality check will always fail here, no?

lennerd avatar Nov 22 '22 16:11 lennerd

@lennerd the fix may be to go back to injecting updaters into components via react internals rather than wrapping all components. I've been using and testing a local version of that approach this week, and it's working in dev and prod builds using React Router 6.4.

developit avatar Nov 22 '22 17:11 developit

Am able to solve the issue by rolling back the router version to 6.0.0

gokul-waterlabs avatar Dec 05 '22 14:12 gokul-waterlabs

@developit do you have any plan to publish your local work/fix?

salamaashoush avatar Jan 04 '23 01:01 salamaashoush

@developit uff, okay. Just had another, quite similar issue, which was really hard to debug (actually was revisting it again after weeks).

Essentially React was warning me about an unmounted component updating its state. Via the debugger I was able to see why: my root component (HistoryRouter from react-router-dom) was unmounting the complete tree underneath it. Turns out: I was lazy loading @preact/signals-react. So when triggering a navigation and rerendering the root component (HistoryRouter) React essentially found new child component type (a proxy from @preact/signals-react) and unmounted the whole thing.

So long story short: what is your progress with the fix? 😬

lennerd avatar Feb 10 '23 14:02 lennerd

I've been using this "lite" version of the React adapter in the meantime, which doesn't include the Signals optimizations, but works with all React libraries and versions: https://gist.github.com/developit/f957536487a84dee334cfeb53748f865

@lennerd as a fix for the lazy-loading issue, you can add a static import for @preact/signals-react to your root component (<App />).

developit avatar Feb 10 '23 17:02 developit

@developit Thats what I'm doing right now and its working just fine. Still not quite what I was hoping for when doing code splitting. 😉

In any case, thanks for the gist! Nice to see how easy the core package can be included in React. I currently have a quite complex setup with multiple class having signal and computed properties, so it's easier and less error prone to use the original package. And work around the error described here until some fix was released.

lennerd avatar Feb 10 '23 18:02 lennerd

@developit Any updates to fix the issue?

jibinmathew69 avatar Mar 07 '23 17:03 jibinmathew69

As @gokul-waterlabs suggested, I tried using router 6.0.0 and it seems to work, any explainations on why it worked?

jibinmathew69 avatar Mar 07 '23 18:03 jibinmathew69

Any update on fix?

sunderbharath85 avatar Mar 15 '23 03:03 sunderbharath85

+1 for fix on this! ❤️

StephenSHorton avatar Mar 19 '23 23:03 StephenSHorton

I've been using this "lite" version of the React adapter in the meantime, which doesn't include the Signals optimizations, but works with all React libraries and versions: https://gist.github.com/developit/f957536487a84dee334cfeb53748f865

@lennerd as a fix for the lazy-loading issue, you can add a static import for @preact/signals-react to your root component (<App />).

Previously I can put signal(0) in a utils file and call it everywhere I want but now with the 'lite' version. We can only use it as hooks which need to be in a React component instead a util fn. Am I understanding that correctly? Or how do we pass useSignal between components without prop drillings

jaydenintusurg avatar Mar 27 '23 17:03 jaydenintusurg

@jaydenintusurg in the lite version from my gist, you can use signal() anywhere, then pass those values through useSignal() to access them. You don't have to create signals with the hooks, only access them.

developit avatar Mar 27 '23 21:03 developit

This version of signals, is kinda atom state management, without main feature of signals. Are you want to deprecate version for react? Do you need help?

XantreDev avatar Mar 28 '23 15:03 XantreDev