@xiao-ying/miniapp-hooks
v1.1.0
Published
React hooks for @xiao-ying/miniapp-sdk to build reactive miniapp UIs
Downloads
90
Readme
@xiao-ying/miniapp-hooks
基于 @xiao-ying/miniapp-sdk 的通用 React hooks(不包含 cloud/snapshot)。
安装
pnpm add @xiao-ying/miniapp-hooks @xiao-ying/miniapp-sdk reactHook 列表
useBrightness():订阅原生/浏览器亮度变化,返回{ brightness, isDark }。useForeground():订阅前后台变化,返回{ isForeground }。useMediaQuery():暴露注入的mediaQuery(安全区、DPR、尺寸)。useStorage(key, options?):与xy.onStorageChange同步,提供value、loading、error以及setValue/remove/refresh。useShare(input?):注册分享处理器,自动补齐默认 path/title。useHapticFeedback(options?)(别名useHapticPress):封装xy.vibrate。useSdkError(options?):订阅xy.onError,缓存过滤后的错误并返回latest、errors、clear。useAuthState():订阅登录态变化,返回{ authState, hasAuthPermission, isSignedIn, userId }。
cloud/snapshot hooks 已迁移到:
@xiao-ying/miniapp-cloud-kit(core API)@xiao-ying/miniapp-cloud-hooks(SWR hooks)
快速示例
import { useBrightness, useStorage } from '@xiao-ying/miniapp-hooks'
const Preferences = () => {
const { brightness, isDark } = useBrightness()
const theme = isDark ? 'dark' : 'light'
const storage = useStorage<string>('nickname', { defaultValue: '游客' })
return (
<div data-theme={theme}>
<p>当前模式: {brightness}</p>
<input
value={storage.value ?? ''}
onChange={(e) => storage.setValue(e.target.value)}
placeholder="输入昵称"
/>
</div>
)
}