Learn how to detect the device type (mobile, tablet, desktop) on React Server Side Rendering application with Next.js framework.
This article provides a step-by-step guide on how to detect device types in a Next.js application using the react-device-detect library and the browser's userAgent property.
To detect device types in a Next.js application, you can use the react-device-detect library and the browser's userAgent property. Here's a step-by-step guide:
Install the library:
npm i react-device-detectImport the necessary components:
import { getSelectorsByUserAgent } from 'react-device-detect';Get the user agent on the server-side:
function getDeviceSelectors(userAgent) {
return getSelectorsByUserAgent(userAgent);
}
export async function getServerSideProps(context) {
const userAgent = context.req.headers['user-agent'];
const deviceSelectors = getDeviceSelectors(userAgent);
return {
props: {
deviceSelectors,
},
};
}Use the device selectors in your component:
function MyComponent({ deviceSelectors }) {
return (
<div>
{deviceSelectors.isMobile && <p>You are on a mobile device</p>}
{deviceSelectors.isDesktop && <p>You are on a desktop device</p>}
</div>
);
}
export default MyComponent;Explanation:
getSelectorsByUserAgent function from react-device-detect provides information about the device based on the user agent.getServerSideProps, we retrieve the user agent from the request headers and pass it to getDeviceSelectors.deviceSelectors object contains boolean properties like isMobile, isDesktop, etc., indicating the device type.deviceSelectors as props to our component.deviceSelectors properties.Note:
window.matchMedia API as an alternative.This code is a Next.js page component that uses the react-device-detect library to determine the user's device type (mobile or desktop) and display a corresponding message. It does this by fetching the user agent from the request headers on the server side and using the library to get device selectors. These selectors are then passed as props to the component, which uses them to conditionally render the appropriate message.
// pages/index.js
import { getSelectorsByUserAgent } from 'react-device-detect';
function getDeviceSelectors(userAgent) {
return getSelectorsByUserAgent(userAgent);
}
export async function getServerSideProps(context) {
const userAgent = context.req.headers['user-agent'];
const deviceSelectors = getDeviceSelectors(userAgent);
return {
props: {
deviceSelectors,
},
};
}
function MyComponent({ deviceSelectors }) {
return (
<div>
{deviceSelectors.isMobile && <p>You are on a mobile device</p>}
{deviceSelectors.isDesktop && <p>You are on a desktop device</p>}
</div>
);
}
export default MyComponent;To use this code:
Install the library:
npm i react-device-detectCreate a new Next.js application:
npx create-next-app my-device-detect-appReplace the contents of pages/index.js with the code provided above.
Start the development server:
npm run devNow, when you access your Next.js application in a browser, it will display a message indicating whether you are on a mobile or desktop device.
react-device-detect library offers a wide range of device selectors, including isBrowser, isAndroid, isIOS, isTablet, and more. Refer to the library's documentation for a complete list.| Step | Description | Code |
|---|---|---|
1. Install react-device-detect
|
Install the library using npm. | npm i react-device-detect |
| 2. Import Components | Import the getSelectorsByUserAgent function. |
import { getSelectorsByUserAgent } from 'react-device-detect'; |
| 3. Server-Side User Agent Detection | Retrieve user agent from request headers and get device selectors. | javascript function getDeviceSelectors(userAgent) { return getSelectorsByUserAgent(userAgent); } export async function getServerSideProps(context) { const userAgent = context.req.headers['user-agent']; const deviceSelectors = getDeviceSelectors(userAgent); return { props: { deviceSelectors, }, }; } |
| 4. Utilize Device Selectors in Component | Conditionally render content based on device type using deviceSelectors properties. |
javascript function MyComponent({ deviceSelectors }) { return ( <div> {deviceSelectors.isMobile && <p>You are on a mobile device</p>} {deviceSelectors.isDesktop && <p>You are on a desktop device</p>} </div> ); } export default MyComponent; |
Key Points:
getSelectorsByUserAgent function provides device information based on user agent.deviceSelectors object contains boolean properties (e.g., isMobile, isDesktop) for device type.window.matchMedia API.This comprehensive guide detailed how to implement device detection in Next.js applications using the react-device-detect library and server-side rendering. By leveraging the user agent, developers can tailor content and user experiences based on device type. The provided code snippets offer a practical starting point for incorporating device detection into your Next.js projects. Remember to consider the ethical implications, potential limitations, and best practices associated with device detection to ensure responsible and effective implementation.
react-device-detect - npm | Detect device type and render your component according to it. Latest version: 2.2.3, last published: a year ago. Start using react-device-detect in your project by running npm i react-device-detect. There are 1134 other projects in the npm registry using react-device-detect.
React/NextJS โ SSR and Responsive Design | by Fred Wong ... | I have ran into this for trying to build the responsive design with SSR feature in NextJS.
The Best Way to Detect Mobile Devices in Next.js 14 | Stackademic | I am going to show it to you in this article along with the best way to get the screen width in client components