-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
MediaQueryList.addListener Deprecated #98
Comments
I just realized that using |
Thanks @DaleSeo! If you have a suggestion for improving this, that would be welcome :) |
@kentcdodds I think I'm interested in looking for a solution but I'm a bit concerned that if we got it fixed, your video wouldn't show the latest code. Would it be okay with you? |
Yeah, because that code isn't the focus of the video do it's fine. |
Created issues to request adding support for addEventListener/removeEventListener. |
Hi everyone, I was just thinking, since import { useState, useEffect, useDebugValue } from "react"
const formatDebugValue = ({ minWidth, maxWidth, isWithin }) => {
if (maxWidth === Infinity) {
return `(min-width: ${minWidth}px) => ${isWithin}`
}
if (minWidth === 0) {
return `(max-width: ${maxWidth}px) => ${isWithin}`
}
return `(max-width: ${maxWidth}px) and (min-width: ${minWidth}px) => ${isWithin}`
}
function useWithinWindowWidth(minWidth, maxWidth) {
const [width, setWidth] = useState(undefined)
const isWithin = width && width >= minWidth && width <= maxWidth
useDebugValue({ minWidth, maxWidth, isWithin }, formatDebugValue)
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth)
window.addEventListener("resize", handleResize)
// Call handler right away so state gets updated with initial window size
handleResize()
return () => window.removeEventListener("resize", handleResize)
}, [])
return isWithin
}
function Box() {
const isBig = useWithinWindowWidth(1000, Infinity)
const isMedium = useWithinWindowWidth(700, 999)
const isSmall = useWithinWindowWidth(0, 699)
const color = isBig ? "green" : isMedium ? "yellow" : isSmall ? "red" : null
return <div style={{ width: 200, height: 200, backgroundColor: color }} />
}
function App() {
return <Box />
}
export default App What do you think? Thanks! |
Thanks for the idea @mithi! When I update this workshop I may find an alternative hook for this example, or maybe I'll write my own polyfill. |
I came across the following warning when I opened
src/excercise/06.js
in my editor.After a bit of googling, I found the following guideline on MDN.
https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList
The text was updated successfully, but these errors were encountered: