usePlatform
跟蹤用戶當前平台的信息。
usePlatform 偵測使用者的作業系統平台(iOS、Android、macOS、Windows、Linux),透過解析 navigator.userAgent 字串。它回傳一個平台識別字串。可在 SSR 時傳入 userAgent prop 以支援伺服器端偵測。
使用場景
- 根據使用者平台調整 UI(例如在 iOS 上顯示 App Store 連結,在 Android 上顯示 Google Play 連結)
- 為不同平台提供特定的安裝或設定說明
- 收集平台相關的分析資料以了解使用者基礎
注意事項
- SSR 安全:接受可選的
userAgentprop 用於伺服器端渲染。若未提供,在伺服器上回傳"unknown"作為平台。 - User agent 解析:偵測依賴
navigator.userAgent,可能被偽造。用於漸進增強而非安全性關鍵決策。 - 相關 hooks:另請參閱
useScreenSafeArea獲取安全區域內縮值,以及useMobileLandscape偵測行動裝置方向。
Usage
Live Editor
function Demo() { const {platform} = usePlatform(); return <p>平台: {platform}</p>; };
Result
API
UsePlatformProps
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| userAgent | 服务端渲染时,需要传递 userAgent | string | - |
usePlatform
Returns
UsePlatformReturn: 和平台相关的对象
Arguments
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| props | - | UsePlatformProps | undefined | - |
UsePlatformReturn
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| platform | 平台 | Platform (必填) | - |
| isInMiniProgram | 是否在小程序中 | () => boolean (必填) | - |
| isInWechat | 是否在微信中 | () => boolean (必填) | - |
| isiPhoneX | 是否是 iPhoneX | () => boolean (必填) | - |
Platform
export type Platform = 'ios' | 'android' | 'unknown';