Luke Brandon Farrell’s Post

If you’ve ever tried to use createPortal in React Native, you probably ran into challenges. On the web, it’s the standard way to render children into a different part of the DOM, but in React Native? It’s been a missing piece of the puzzle for years, forcing us to rely on workarounds like @gorhom/portal or the 𝗿𝗲𝗮𝗰𝘁-𝗻𝗮𝘁𝗶𝘃𝗲-𝘁𝗲𝗹𝗲𝗽𝗼𝗿𝘁 library we recently talked about. But things are changing. 𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 𝗣𝗥 #𝟱𝟱𝟰𝟲𝟯 just dropped: experimental support for 𝗥𝗲𝗮𝗰𝘁 𝗣𝗼𝗿𝘁𝗮𝗹𝘀 𝗶𝗻 𝗙𝗮𝗯𝗿𝗶𝗰. 𝗪𝗵𝗮𝘁’𝘀 𝗵𝗮𝗽𝗽𝗲𝗻𝗶𝗻𝗴? Historically, portals were "DOM magic." Since React Native doesn't have a DOM, the architecture didn't quite know how to handle them. This PR leverages the 𝗡𝗲𝘄 𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 (Fabric) to intercept shadow tree commits and manually mount portal children into the native view hierarchy. It’s essentially teaching the Fabric renderer how to handle createPortal natively. ➡️ 𝗧𝗿𝘂𝗲 𝗥𝗲𝗮𝗰𝘁 𝗣𝗮𝗿𝗶𝘁𝘆: You can finally write code that looks and behaves like standard React, making cross-platform (Web/Mobile) components much easier to maintain. ➡️ 𝗖𝗼𝗻𝘁𝗲𝘅𝘁 𝗣𝗿𝗲𝘀𝗲𝗿𝘃𝗮𝘁𝗶𝗼𝗻: Unlike some current JS-heavy hacks, this approach is designed to keep React context and event bubbling intact. ➡️ 𝗟𝗲𝗮𝗻𝗲𝗿 𝗔𝗽𝗽𝘀: If this makes it into core, we can stop installing third-party libraries just to show a simple tooltip or modal. We literally just shared how 𝗿𝗲𝗮𝗰𝘁-𝗻𝗮𝘁𝗶𝘃𝗲-𝘁𝗲𝗹𝗲𝗽𝗼𝗿𝘁 is the best way to handle this right now. If this PR lands in a stable release, it might eventually make those specialised libraries obsolete. That’s the nature of the ecosystem—today's essential library becomes tomorrow's built-in feature. Before you go deleting your third-party libraries, keep in mind: 𝘁𝗵𝗶𝘀 𝗶𝘀 𝘀𝘁𝗶𝗹𝗹 𝗶𝗻 𝘁𝗵𝗲 𝗣𝘂𝗹𝗹 𝗥𝗲𝗾𝘂𝗲𝘀𝘁 𝘀𝘁𝗮𝗴𝗲. It’s highly experimental and involves changes to the core React renderer files. #ReactNative #Fabric #ReactJS #Portals #OpenSource #MobileDev #JavaScript #TypeScript #SoftwareEngineering #DevTools #Meta

  • No alternative text description for this image

Source: https://github.com/facebook/react-native/pull/55463 If you’re a React Native developer and want to stay ahead of the curve (with a laugh or two) on the latest updates—without spending hours sifting through release notes. Catch the latest in our newsletter, The React Native Rewind 👇 https://thereactnativerewind.com

Like
Reply

To view or add a comment, sign in

Explore content categories