1 March 2025

In Expo, you can conditionally render components based on the platform (web or mobile) using the Platform module from react-native. Here's an example of how to load a component only for web and another only for mobile.

Excluding mobile-only components (i.e., components that are specifically designed for mobile platforms like iOS/Android) from being included in the web bundle when working with an Expo project that targets both web and mobile platforms. In such a case, conditional imports or platform-specific code splitting are essential to ensure that only web-compatible components are included in the web bundle and mobile-specific components are excluded from the web. Otherwise, trying to import mobile-specific components into the web environment can cause errors or bloated web bundles.

Source code viewer
  1. import { Platform } from 'react-native';
  2.  
  3. // Conditionally import components based on the platform.
  4. let Web;
  5. let Mobile;
  6. if (Platform.OS === 'web') {
  7. Web = require('./src/web').default;
  8. } else {
  9. Mobile = require('./src/mobile').default;
  10. }
  11.  
  12. export default function App() {
  13. return (
  14. <NavigationContainer>
  15. {Platform.OS === 'web' ? <Web /> : <Mobile />}
  16. </NavigationContainer>
  17. );
  18. }
  19.  
Programming Language: ECMAScript