Optional Imports in React Native
Optional imports are useful when a React Native app has platform-specific code, feature-gated screens, optional assets, or native modules that should only load in one environment. They are also easy to misuse. Metro still needs to know what files can be bundled, so arbitrary runtime strings are not a good import strategy.

Quick Answer
Use explicit patterns that Metro and TypeScript can understand:
- platform-specific files for iOS, Android, native, or web differences;
Platform.selectfor small platform branches;- static module maps for optional assets or screens;
import()for known modules that can be lazy-loaded;import typewhen you only need TypeScript types.
Avoid production code that tries to load random dependency names with
try/catch require(...). If a dependency is required for a feature, declare it
in package.json, install it through the project's package manager, and rebuild
native apps when native code is involved.
For current local setup, use Set Up Your React Native Development Environment and Current React Native Stack. For bundler issues, use Metro and Bundler Errors.
Prefer Platform-Specific Files for Real Platform Differences
When the implementation is meaningfully different by platform, create separate files and import the module without the platform suffix:
src/services/share.native.ts
src/services/share.web.ts
src/components/DatePicker.ios.tsx
src/components/DatePicker.android.tsx
Then import normally:
import { shareListing } from './services/share';
import DatePicker from './components/DatePicker';
Metro resolves the right file for the current target. This keeps your component clean and prevents unsupported native code from being loaded on the wrong platform.