DEV Community

Cover image for Creating Truly Responsive UI in React Native with react-native-responsive-dimention
babar bilal
babar bilal

Posted on

Creating Truly Responsive UI in React Native with react-native-responsive-dimention

Introduction

Building a responsive UI in React Native can be complex due to variations in screen sizes, resolutions, and aspect ratios. The react-native-responsive-dimension library simplifies this by offering dynamic scaling utilities for adaptive and scalable layouts.

This article explains how to use this library to achieve a seamless responsive UI in React Native applications.

Why Responsive Design Matters in React Native

Unlike web development, where media queries help handle different screen sizes, React Native apps run across a wide range of devices, from small phones to large tablets. Without a reliable responsive approach, designs may appear inconsistent.

react-native-responsive-dimension provides dynamic scaling functions to ensure a uniform experience across all devices.

Features of react-native-responsive-dimension

  • Adaptive width and height scaling
  • Dynamic font scaling
  • Device type detection (Phone vs. Tablet)
  • Percentage-based width and height calculations
  • Real-time screen dimension updates with a custom hook

Installation

Install the package using npm or yarn:

npm i react-native-responsive-dimension
Enter fullscreen mode Exit fullscreen mode

or

yarn add react-native-responsive-dimension
Enter fullscreen mode Exit fullscreen mode

Getting Started

Import the necessary functions into your React Native component:

import {
  rw,   // Responsive width
  rh,   // Responsive height
  rf,   // Responsive font size
  wp,   // Responsive width percentage
  hp,   // Responsive height percentage
  isTablet, // Detect if device is a tablet
  useResponsive // Hook for real-time updates
} from 'react-native-responsive-dimension';
Enter fullscreen mode Exit fullscreen mode

Responsive Width & Height

To make a component’s size responsive:

<View style={{ width: rw(100), height: rh(200) }}>
  <Text style={{ fontSize: rf(16) }}>Responsive Design</Text>
</View>
Enter fullscreen mode Exit fullscreen mode

Dynamic Font Sizing

<Text style={{ fontSize: rf(20) }}>This is responsive text</Text>
Enter fullscreen mode Exit fullscreen mode

Checking if Device is a Tablet

if (isTablet()) {
  // Apply tablet-specific styles
}
Enter fullscreen mode Exit fullscreen mode

Using Percentage-Based Sizing

<View style={{ width: wp(50), height: hp(30) }}>
  <Text>50% width and 30% height of the screen</Text>
</View>
Enter fullscreen mode Exit fullscreen mode

Real-Time Responsive Hook

import { useResponsive } from 'react-native-responsive-dimension';

const MyComponent = () => {
  const { rw, rh, wp, hp } = useResponsive();

  return (
    <View style={{ width: rw(80), height: rh(40) }}>
      <Text>Responsive content here</Text>
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

API Overview

rw(width: number): number

Scales the width relative to the screen size.

rh(height: number): number

Scales the height relative to the screen size.

rf(size: number, factor?: number): number

Scales font size dynamically based on the screen scaling factor.

wp(percentage: number): number

Calculates a width based on a percentage of the screen width.

hp(percentage: number): number

Calculates a height based on a percentage of the screen height.

isTablet(): boolean

Detects whether the device is a tablet.

useResponsive()

A hook providing real-time dimension updates.

Conclusion

The react-native-responsive-dimension package makes it easy to create truly responsive designs in React Native. By leveraging its utility functions and hooks, developers can ensure a consistent user experience across devices without manually adjusting styles for different screen sizes.

Check out the package on GitHub and install it via npm to start building better, more responsive apps today!

πŸ”— Get Started Today!

πŸ‘‰ GitHub: https://github.com/babarbilal56/react-native-responsive-dimention

πŸ‘‰ LinkedIn: www.linkedin.com/in/babarbilal56

πŸ‘‰ Npm: https://www.npmjs.com/package/react-native-responsive-dimention

Top comments (0)