项目作者: noahflk

项目描述 :
Lightweight hook for responsive breakpoints in React components
高级语言: TypeScript
项目地址: git://github.com/noahflk/react-breakout.git
创建时间: 2020-07-19T14:42:19Z
项目社区:https://github.com/noahflk/react-breakout

开源协议:MIT License

下载



React Breakout


Lightweight hook for responsive breakpoints in your React components





Installation

NPM

  1. npm install react-breakout

Yarn

  1. yarn add react-breakout

Usage

The useBreakpoint hook returns true when the window width is higher than the selected width.

  1. import { useBreakpoint } from "react-breakout";
  2. const App = () => {
  3. const isDesktop = useBreakpoint("md");
  4. return (
  5. <div>
  6. <p>{isDesktop ? "Yes" : "No"}</p>
  7. </div>
  8. );
  9. };

The useBreakpoint hook accepts either a number as the pixel width threshold or on of the following predefined breakpoints:

  1. const breakpoints = {
  2. sm: 640,
  3. md: 768,
  4. lg: 1024,
  5. xl: 1280,
  6. };

API

useBreakpoint

  1. const isAboveThreshold = useBreakpoint("lg");
  2. // OR
  3. const isAboveThreshold = useBreakpoint(1024);

Returns

  • isAboveThreshold: boolean
    • True if the window width is above the selected width

Arguments

  • breakpoint: "sm" | "md" | "lg" | "xl" | number
    • Threshold for window size breakpoint