简体   繁体   English

useMediaQuery 打字稿错误(条件将始终返回 true,因为该函数始终被定义)

[英]useMediaQuery typescript error (condition will always return true since the function is always defined)

I'm trying to use useMediaQuery in my react app for my responsive js media queries.我正在尝试在我的响应应用程序中使用useMediaQuery来进行响应式 js 媒体查询。 I'm using typescript version 4.0.3.我正在使用打字稿版本 4.0.3。

The code is as follows:代码如下:

import { useState, useEffect } from 'react'

const useMediaQuery = (query: string) => {
    const [match, setMatch] = useState(false)

    useEffect(() => {
        const updateMatch = () => setMatch(window.matchMedia(query).matches)

        updateMatch()
        if (window.matchMedia(query).addEventListener) {
            window.matchMedia(query).addEventListener('change', updateMatch)
        } else {
            window.matchMedia(query).addListener(updateMatch)
        }
        return () => {
            if (window.matchMedia(query).removeEventListener) {
                window.matchMedia(query).removeEventListener('change', updateMatch)
            } else {
                window.matchMedia(query).removeListener(updateMatch)
            }
        }
    }, [query])

    return match
}

export default useMediaQuery

The error message I'm getting is:我收到的错误消息是:

This condition will always return true since the function is always defined. Did you mean to call it instead? TS2774

updateMatch()
  if (window.matchMedia(query).addEventListener) {
       ^

在此处输入图片说明

Does anyone have any ideas on how to fix this code to get it working?有没有人对如何修复此代码以使其正常工作有任何想法?

Thanks谢谢

Maybe you just don`t need this condition.也许你只是不需要这个条件。 On my env I have error在我的环境中我有错误

deprecated MediaQueryList.removeListener

so you will have another issue when fix current所以在修复电流时你会遇到另一个问题

if(window.matchMedia(query).addEventListener) {...}

This will always return the function reference.这将始终返回函数引用。 So it will always execute the if block.所以它会一直执行 if 块。 That's why typescript is throwing error.这就是打字稿抛出错误的原因。 So instead of using if/else, you could directly call the methods.因此,您可以直接调用这些方法,而不是使用 if/else。

window.matchMedia(query).addEventListener('change', updateMatch)
window.matchMedia(query).addListener(updateMatch)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 错误 此条件将始终返回“真”,因为类型没有重叠 - Typescript error This condition will always return 'true' since the types have no overlap 打字稿错误此条件将始终返回“true”,因为以下类型没有重叠 - Typescript error This condition will always return 'true' since the below type has no overlap Typescript 错误条件将始终返回“真”,因为“字符串”和“0”类型没有重叠 - Typescript error condition will always return 'true' since the types 'string' and '0' have no overlap TS2774:此条件将始终返回 true,因为始终定义此 function。 你的意思是改为调用它吗 - TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead 函数中带有字符串的打字稿条件总是返回 false - Typescript condition with strings in function always return false 模板中的枚举 - 此条件将始终返回“true”,因为“RequestStatus.Done”和“RequestStatus.Rejected”类型没有重叠 - Enums in templates - This condition will always return 'true' since the types 'RequestStatus.Done' and 'RequestStatus.Rejected' have no overlap Function 似乎总是返回 true - Function seems to always return true 错误 - ''此条件将始终返回 false,因为类型 ''string'' 和 ''number'' 没有重叠'' - Error - ''This condition will always return false since the types ''string'' and ''number'' have no overlap'' 此条件将始终返回 false,因为 Light 和 Dark 类型没有重叠 - This condition will always return false since the types Light and Dark have no overlap 为什么这个条件总是正确的? - Why this condition is always true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM