-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "useBreakpoint" hook #279
Comments
Questions
It's about never you have interest in a matched breakpoint name without deriving something from it. To do the latter there is a callback function (hook argument), which replaces the need to return the breakpoint name. I'd probably make the |
i also find this interesting, i did came up with 2 hooks on my lib:
|
I absolutely love the import { useBreakpointChange } from 'atomic-layout'
const Usage = () => {
// Instead of inline side-effect
useBreakpointChange((currentBreakpointName) => {...})
// Prefer getting the active breakpoint name and doing whatever is needed with it
const breakpointName = useCurrentBreakpoint()
useEffect(() => {...}, [breakpointName])
} @vitordino, could you please share a use case for the |
I think I understood why multiplicity is relevant when matching breakpoints. I've addressed it in the pull request attached to this issue. Basically, it's possible for multiple breakpoints to match the same state of the viewport. The call signature I'm suggesting in the pull request is: import React, { useEffect } from 'react'
import { useCurrentBreakpoints } from 'atomic-layout'
const Usage = () => {
const breakpoints = useCurrentBreakpoints()
useEffect(() => {
if (breakpoints.includes('sm')) {
doAction()
}
}, [breakpoints])
return null
}
But then, once such utility is introduced, I see no difference between it and |
I mainly wonder what is the benefit of the import { useResponsiveQuery } from 'atomic-layout'
const Usage = () => {
// Target a single breakpoint
const isLargeScreen = useResponsiveQuery({ for: 'lg' })
// Or a breakpoint range
const isBiggerThanMedium = useResponsiveQuery({ from: 'md' })
} Effectively one reaches to a hook like It would be nice to have a usage example of |
yep, you caught one idea of using an array on i’m not agains the just some other ways to let users of the lib explore the api (some will prefer only using one of it, but maybe there are best uses for both) 🤷 |
@vitordino, you have a point. While reading your reply I've also realized that
|
What:
I suggest to add the following React hook:
Why:
It should replace current
useBreakpointChange
hook with both:useBreakpointChange
.useBreakpoint
will be called on initial render, whenuseBreakpointChange
is not.Main motivation is that it's currently lengthy to know which breakpoint is met. It requires to have
useState
updating its value onuseBreakpointChange
callback. With the suggested hook added it can be written in a single hook declaration.Usage example:
Getting a breakpoint name
Reacting to breakpoint change
How:
useBreakpointChange
useBreakpoint
The text was updated successfully, but these errors were encountered: