DEV Community

Cover image for Conditional Responsive Design with Ant Design's useBreakpoint Hook
Sarwar Hossain
Sarwar Hossain

Posted on • Updated on

Conditional Responsive Design with Ant Design's useBreakpoint Hook

Image description
Step 1: Install Ant Design:
Ensure you have Ant Design installed in your React project. You can install it using:

npm install antd
Enter fullscreen mode Exit fullscreen mode

Step 2: Import useBreakpoint:
In your React component, import the useBreakpoint hook:

import useBreakpoint from "antd/lib/grid/hooks/useBreakpoint";
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize the Hook:
Create a variable using the useBreakpoint hook in your functional component:

const screens = useBreakpoint();
Enter fullscreen mode Exit fullscreen mode

Step 4: Implement Conditional Styling:
Apply conditional styling based on different screen sizes. Here, we change the background color of a button for small screens:

<Button
  style={{
    backgroundColor: screens.sm ? "red" : "white",
  }}
>
  Open
</Button>
Enter fullscreen mode Exit fullscreen mode

Step 5: Device-Specific Content:
Showcase content specific to certain devices using conditional rendering. Display a message for medium and large devices:

{
  screens.md && <button>Medium device content</button>
}
{
  screens.xl && <button>Large device content</button>
}
Enter fullscreen mode Exit fullscreen mode

Finnaly, You can use it for

  • xs,
  • sm,
  • lg,
  • md,
  • xl ,
  • xxl

Top comments (0)