useResponsive
React Hook for getting responsive info.
Overview
React Hook for getting responsive info.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useResponsive.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useResponsive.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useResponsive.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useResponsive.jsonReact Hook for getting responsive info.
Examples
Get responsive info in components
API
interface ResponsiveConfig {
[key: string]: number;
}
interface ResponsiveInfo {
[key: string]: boolean;
}
function configResponsive(config: ResponsiveConfig): void;
function useResponsive(): ResponsiveInfo;Config
The default config is the same as bootstrap:
{
'xs': 0,
'sm': 576,
'md': 768,
'lg': 992,
'xl': 1200,
}If you want to config your own responsive breakpoints, you can use configResponsive:
(Caution: You only need to config it once. Don't call this config function repeatedly.)
configResponsive({
small: 0,
middle: 800,
large: 1200,
});useResetState
useResetState works similar to React.useState, it provides a reset method
useSafeState
It is exactly the same with React.useState , but after the component is unmounted, the setState in the asynchronous callback will no longer be executed to avoid memory leakage caused by updating the state after the component is unmounted.