DEV Community

Cover image for Rendering Components in React Native for Different Platforms
Paulo Messias
Paulo Messias

Posted on

Rendering Components in React Native for Different Platforms

Hey devs! If you've ever found yourself in a situation where you wanted a component to look different on iOS, Android, or the web, you're not alone. But fear not, because today I'm going to share a technique that will greatly simplify your life: using specific imports for each platform in React Native.

Why is this important?

In the past, we used to do a bunch of tedious Platform.OS checks to determine how to render components. It was like, "If it's iOS, do this. If it's Android, do that." It was a lot of work and made our code messy.

The solution? Specific imports for each platform!

With this technique, we export a specific component for each platform directly from our main file. For example, a ButtonIOS just for iOS and a ButtonAndroid just for Android.

Advantages of using specific imports:

  1. Cleaner and more organized code: You know exactly what you're importing for each platform, making your code easier to understand and maintain.

  2. Efficiency in loading: React Native only loads the necessary code for the platform your app is running on, making loading faster and more efficient.

Example with Platform.OS Validation:

If you prefer not to use specific imports, you can also validate Platform.OS directly in the component.

import React from 'react';
import { View, Pressable, Text, Platform } from 'react-native';

const Button = () => {
  return (
    <View>
      {Platform.OS === 'ios' && (
        <Pressable>
          <Text>Button for iOS</Text>
        </Pressable>
      )}
      {Platform.OS === 'android' && (
        <Pressable>
          <Text>Button for Android</Text>
        </Pressable>
      )}
    </View>
  );
};

export default Button;
Enter fullscreen mode Exit fullscreen mode

Example with Specific Imports:

Without specific imports, we would have to deal with complicated Platform.OS checks. But with the right imports, everything becomes much simpler and more straightforward.

First, let’s separate the files: one for Android and another for iOS

iOS:

//button.ios.tsx

import React from 'react';
import { Pressable, Text } from 'react-native';

const ButtonIOS = () => {
  return (
    <Pressable>
      <Text>Button for iOS</Text>
    </Pressable>
  );
};

export default ButtonIOS;
Enter fullscreen mode Exit fullscreen mode

Android:

//button.android.tsx

import React from 'react';
import { Pressable, Text } from 'react-native';

const ButtonAndroid = () => {
  return (
    <Pressable>
      <Text>Button for Android</Text>
    </Pressable>
  );
};

export default ButtonAndroid;
Enter fullscreen mode Exit fullscreen mode

In the button.tsx file, we simply export the specific components for each platform:

//button.tsx

export { default as ButtonIOS } from './button.ios';
export { default as ButtonAndroid } from './button.android';
Enter fullscreen mode Exit fullscreen mode

Then, the magic:

import React from 'react';
import { View, Platform } from 'react-native';
import CustomButton from './button'; // This is where the magic happens!

const Button = () => {
  return (
    <View>
     <CustomButton />
    </View>
  );
};

export default Button;
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Using specific imports for each platform in React Native is a simple and effective way to ensure that your app looks and behaves as desired on each platform. With cleaner, more organized, and more efficient code, you can focus on what really matters: creating great experiences for users.

So why not try this approach in your next project? You won't regret it!

What do you think of this technique? Have you ever used something similar?

Top comments (0)