默认属性错误:预期1个参数,但得到0个

huangapple go评论135阅读模式
英文:

Default prop error: Expected 1 arguments, but got 0

问题

I created this utility to detect mobile using matchMedia.

我创建了这个实用程序来使用matchMedia检测移动设备。

I created a prop to manually adjust the breakpoint if needed. I defined the breakpoint prop as optional and gave a default value in the function.

如果需要,我创建了一个属性来手动调整断点。我将断点属性定义为可选的,并在函数中给了一个默认值。

For some reason I'm required to pass an empty object though when using the util?

但是当使用该实用程序时,出于某种原因,我需要传递一个空对象?

OtherComponent.tsx:
const { isMobile } = DetectMobile({});

其他组件.tsx:
const { isMobile } = DetectMobile({});

If I remove the empty object I get the error:

如果我删除空对象,我会收到错误消息:

Expected 1 arguments, but got 0

预期需要1个参数,但没有提供0个

I'm fairly new to typescript and I'm not entirely sure what I'm doing wrong, I feel like I should not need to pass the empty object and it should pick up on the default value for the breakpoint prop?

我对TypeScript相对较新,我不完全确定我做错了什么,我觉得我不应该需要传递空对象,它应该使用断点属性的默认值?

Here's the util:

这是实用程序:

import { useEffect, useState, useMemo } from "react";

import { useEffect, useState, useMemo } from "react";

type DetectMobileProps = {
breakpoint?: string;
};

type DetectMobileProps = {
breakpoint?: string;
};

export function DetectMobile({ breakpoint = "768px" }: DetectMobileProps) {
export function DetectMobile({ breakpoint = "768px" }: DetectMobileProps) {

const [isMobileDevice, setIsMobile] = useState(false);

const [isMobileDevice, setIsMobile] = useState(false);

useEffect(() => {
useEffect(() => {

  1. const mediaWatcher = window.matchMedia(`(max-width: ${breakpoint})`);
  2. const mediaWatcher = window.matchMedia(`(max-width: ${breakpoint})`);
  3. setIsMobile(mediaWatcher.matches);
  4. setIsMobile(mediaWatcher.matches);
  5. function updateIsMobile(event) {
  6. function updateIsMobile(event) {
  7. setIsMobile(event.matches);
  8. setIsMobile(event.matches);
  9. }
  10. }
  11. if (mediaWatcher.addEventListener) {
  12. if (mediaWatcher.addEventListener) {
  13. mediaWatcher.addEventListener("change", updateIsMobile);
  14. mediaWatcher.addEventListener("change", updateIsMobile);
  15. }
  16. }

}, []);

}, []);

const isMobile = useMemo(() => {
const isMobile = useMemo(() => {

  1. return isMobileDevice;
  2. return isMobileDevice;

}, [isMobileDevice]);

}, [isMobileDevice]);

return {
return {
isMobile
isMobile
};
};
}
}

Any advice/help?

有任何建议或帮助吗?

英文:

I created this utility to detect mobile using matchMedia.

I created a prop to manually adjust the breakpoint if needed. I defined the breakpoint prop as optional and gave a default value in the function.

For some reason I'm required to pass an empty object though when using the util?

  1. OtherCompnent.tsx:
  2. const { isMobile } = DetectMobile({});

If I remove the empty object I get the error:

  1. Expected 1 arguments, but got 0

I'm fairly new to typescript and I'm not entirely sure what I'm doing wrong, I feel like I should not need to pass the empty object and it should pick up on the default value for the breakpoint prop?

Here's the util:

  1. import { useEffect, useState, useMemo } from "react";
  2. type DetectMobileProps = {
  3. breakpoint?: string;
  4. };
  5. export function DetectMobile({ breakpoint = "768px" }: DetectMobileProps) {
  6. const [isMobileDevice, setIsMobile] = useState(false);
  7. useEffect(() => {
  8. const mediaWatcher = window.matchMedia(`(max-width: ${breakpoint})`);
  9. setIsMobile(mediaWatcher.matches);
  10. function updateIsMobile(event) {
  11. setIsMobile(event.matches);
  12. }
  13. if (mediaWatcher.addEventListener) {
  14. mediaWatcher.addEventListener("change", updateIsMobile);
  15. }
  16. }, []);
  17. const isMobile = useMemo(() => {
  18. return isMobileDevice;
  19. }, [isMobileDevice]);
  20. return {
  21. isMobile
  22. };
  23. }

Any advice/help?

答案1

得分: 1

你的DetectMobile函数需要一个参数,因为props本身不是可选的。

如果你想要一个真正可选的prop,你需要像这样做:

  1. export function DetectMobile(prop?: string) => {
  2. // 代码的其余部分
  3. // 将"breakpoint"调整为"prop.breakpoint"
  4. }
英文:

Your DetectMobile function requires a parameter because the props themselves are not optional.

If you wanted a truly optional prop, you'd have to do something like this:

  1. export function DetectMobile(prop?: string) => {
  2. // rest of code
  3. // adjust "breakpoint" to "prop.breakpoint"
  4. }

huangapple
  • 本文由 发表于 2023年6月9日 02:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定