When you’re building a React Native app, it’s unlikely that you’ll be able to use the same code for Android and iOS.
That’s fine—you don’t have to. In fact, it’s better if you don’t! Each platform has its requirements and quirks. If you try to write your app code in a way that works for both platforms, your code will likely be clunky. It won’t be easy to maintain and debug and will take longer than necessary to run.
The good news is that React Native framework lets you handle platform-specific code. The bad news is that it can be confusing at first—but we’re here to help!
There are two ways React Native experts handle platform-specific code.
Write platform-specific code in the same file.
This is the easiest way to handle platform-specific code. Write your code in a single file. Include platform-specific code blocks wherever necessary. This approach works well if you only need to do something different on iOS versus Android. It can get messy if you have other differences unrelated to platforms.
Create a separate file for each platform.
This is the most common approach for people with more than platform-specific code. Create a separate .ios.js file and a .android.js file to hold your platform-specific code. Then, import them into your main index.js file when needed. This approach works if you have other differences between platforms unrelated to UI.