usePlatform
React hook to tracked the platform of the user.
usePlatform detects the user’s device platform by parsing the user agent string via navigator.userAgent. It returns an object with a platform value ("ios", "android", or "unknown") along with helper methods: isInMiniProgram(), isInWechat(), and isiPhoneX(). For SSR, you can pass a userAgent string to enable server-side detection.
When to Use
- Rendering platform-specific UI or behavior (e.g., different scroll handling on iOS vs. Android)
- Detecting WeChat or mini-program environments for specialized integrations
- Adjusting safe-area insets or notch handling on iPhone X and newer models
Notes
- SSR-safe: Accepts an optional
userAgentprop for server-side rendering. Without it, returns"unknown"as the platform on the server. - User agent parsing: Detection relies on
navigator.userAgent, which can be spoofed. Use for progressive enhancement, not security-critical decisions. - Related hooks: See also
useScreenSafeAreafor safe-area inset values anduseMobileLandscapefor orientation detection on mobile devices.
Usage
Live Editor
function Demo() { const {platform} = usePlatform(); return <p>platfrom: {platform}</p>; };
Result
API
UsePlatformProps
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| userAgent | When server rendering, you need to pass userAgent | string | - |
usePlatform
Returns
UsePlatformReturn: object that related to platform
Arguments
| Argument | Description | Type | DefaultValue |
|---|---|---|---|
| props | - | UsePlatformProps | undefined | - |
UsePlatformReturn
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| platform | platform | Platform (Required) | - |
| isInMiniProgram | Whether in mini program | () => boolean (Required) | - |
| isInWechat | whether in wechat | () => boolean (Required) | - |
| isiPhoneX | whether is iPhoneX | () => boolean (Required) | - |
Platform
export type Platform = 'ios' | 'android' | 'unknown';