Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SwarajDashDev/32df00440a9ffc58a15709a8a0537b77 to your computer and use it in GitHub Desktop.

Select an option

Save SwarajDashDev/32df00440a9ffc58a15709a8a0537b77 to your computer and use it in GitHub Desktop.
Leverage a single codebase to power both mobile and web UIs using React Native Web inside a Yarn workspaces monorepo.

Insight

A monorepo lets you keep shared UI components, types, and utilities in one place, reducing duplication between a React Native app and its web counterpart. By adding react-native-web and configuring Metro alongside Webpack, the same component can render natively on iOS/Android and as HTML/CSS in the browser. The trick is to abstract platform‑specific differences with Platform.select or conditional imports, so the component stays thin and testable.

Example

// packages/ui/src/Button.tsx
import { Platform, TouchableOpacity, Text } from 'react-native';

export const Button = ({ title, onPress }: { title: string; onPress: () => void }) => (
  <TouchableOpacity
    onPress={onPress}
    style={Platform.select({
      ios: { backgroundColor: '#0a84ff' },
      android: { backgroundColor: '#3ddc84' },
      web: { backgroundColor: '#ff5252', cursor: 'pointer' },
    })}
  >
    <Text style={{ color: '#fff' }}>{title}</Text>
  </TouchableOpacity>
);

Takeaway

Set up a Yarn workspace with packages/ui for shared components, then import them from both the React Native app and the Next.js web project. Keep platform quirks isolated with Platform.select to maintain a single source of truth and dramatically speed up UI consistency across platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment