discussions-and-proposals icon indicating copy to clipboard operation
discussions-and-proposals copied to clipboard

React Native Portals

Open intergalacticspacehighway opened this issue 4 years ago • 6 comments

Introduction

React Native portals would allow to render a component into a different native hierarchy while preserving the context and react tree hierarchy. This feature already exists in the source code and works fine on Android but throws this error on iOS.

I have created a repo that makes this feature work on iOS and Android in the current non-fabric architecture.

Details

I tried a fix on iOS by commenting this assertion. This assertion states that the container should be a root shadow view instance. The above error doesn't occur if we pass rootTag (e.g. one which we get in logs on app start) in createPortal API. However, to make the Portal work consistently on iOS and Android we might need to remove this assertion or find an alternate way (I am not sure why this assertion is added).

Discussion points

  • Is there an alternate way to keep the assertion and make Portals work on iOS?
  • The Portal API would be a great addition for usecases like Toasts, Alerts, BottomSheets etc and can be used as an alternate to multiple modal usecases.
  • I am yet to test this on fabric architecture. Fabric uses UIManager.cpp and this assertion exists on RCTUIManager. I'll try to test it.

Since this feature is not yet exported officially, I thought of starting it as a discussion. :)

First-class portals would be great, especially with the limitations modals have now.

nandorojo avatar Aug 29 '21 15:08 nandorojo

First-class portals would be great, especially with the limitations modals have now.

If I can add some context, here are a few limitations of the native modal we've discovered while working on React Native Modal:

To be clear: I don't have anything against React Native's built-in modal component. I know that most of the issues reported around it are just limitations of the native modal implementation itself. I think there's a place and time for using the built-in modal component, but in some cases having a way to portal a JavaScript component to the root (while preserving its context) would be handy.

mmazzarolo avatar Aug 29 '21 16:08 mmazzarolo

there's this that could help for a PR https://github.com/callstack/react-native-paper/tree/master/src/components/Portal

pvinis avatar Oct 17 '21 14:10 pvinis

there's this that could help for a PR https://github.com/callstack/react-native-paper/tree/master/src/components/Portal

This portal implementation works entirely in the JavaScript land though, right?

mmazzarolo avatar Oct 17 '21 14:10 mmazzarolo

I use a pure JS implementation similar to react-native-paper one for quite a long time. @mmazzarolo could I kindly ask you, what benefits do you think it would have if it was implemented in native?

savelichalex avatar Feb 14 '22 12:02 savelichalex