useRef 抛出不可分配给类型 LegacyRef<Slider> |不明确的
useRef throws not assignable to type LegacyRef<Slider> | undefined
我正在使用 react-slick-slider,我想要实现的是制作自定义箭头。
所以代码看起来像这样:
const FeedbackSlider = () => {
const [isLargeScreen] = useMediaQuery("(min-width: 1050px)")
const sliderRef = useRef<Slider>()
const prev = () => {
sliderRef.current?.slickPrev()
}
const next = () => {
sliderRef.current?.slickNext()
}
const settings = {
dots: false,
infinite: true,
speed: 500,
slidesToShow: (isLargeScreen ? 2 : 1),
slidesToScroll: 1,
prevArrow: (isLargeScreen && <PrevArrow />),
nextArrow: (isLargeScreen && <NextArrow />)
};
return (
<Box px={['2', '4', '8', '16']} mx='auto' pt='12'>
<Slider ref={sliderRef} {...settings}>
// slider cards...
</Slider>
{!isLargeScreen && (
<Flex alignItems='center' w='5xs' justifyContent='space-between' mx='auto' my='5'>
<PrevArrow onClick={prev} />
<NextArrow onClick={next} />
</Flex>)}
</Box >)
用作类型的 Slider 是从我正在使用的 react-slick 导入的 Slider 组件
我只在 VScode 中收到错误代码在浏览器中正常工作:
No overload matches this call.
Overload 1 of 2, '(props: Settings | Readonly): Slider', gave the following error.
Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'LegacyRef | undefined'.
Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'RefObject'.
Types of property 'current' are incompatible.
Type 'Slider | undefined' is not assignable to type 'Slider | null'.
Type 'undefined' is not assignable to type 'Slider | null'.
Overload 2 of 2, '(props: Settings, context: any): Slider', gave the following error.
Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'LegacyRef | undefined'.
编辑:
No overload matches this call.
Overload 1 of 2, '(props: Settings | Readonly): Slider', gave the following error.
Type '{ children: Element[]; dots: boolean; infinite: boolean; speed: number; slidesToShow: number; slidesToScroll: number; prevArrow: false | Element; nextArrow: false | Element; ref: MutableRefObject<...>; }' is not assignable to type 'Readonly'.
Types of property 'nextArrow' are incompatible.
Type 'false | Element' is not assignable to type 'Element | undefined'.
Type 'false' is not assignable to type 'Element | undefined'.
Overload 2 of 2, '(props: Settings, context: any): Slider', gave the following error.
Type '{ children: Element[]; dots: boolean; infinite: boolean; speed: number; slidesToShow: number; slidesToScroll: number; prevArrow: false | Element; nextArrow: false | Element; ref: MutableRefObject<...>; }' is not assignable to type 'Readonly'.
Types of property 'nextArrow' are incompatible.
Type 'false | Element' is not assignable to type 'Element | undefined'.
看到这部分错误:
Type 'Slider | undefined' is not assignable to type 'Slider | null'. Type 'undefined' is not assignable to type 'Slider | null'.
如果未传递默认值,useRef
会将 undefined
添加到类型。您可能想像这样传递 null:
// bad:
const sliderRef = useRef<Slider>() // type: Slider | undefined
// good
const sliderRef = useRef<Slider | null>(null) // type: Slider | null
关于您的第二个错误,请看一下最后一部分,尝试推断发生了什么:
Types of property 'nextArrow' are incompatible. Type 'false | Element' is not assignable to type 'Element | undefined'.
似乎 nextArrow
接受 Element
或 undefined
。目前你传递的箭头是这样的:
prevArrow: (isLargeScreen && <PrevArrow />),
nextArrow: (isLargeScreen && <NextArrow />)
请注意,如果 isLargeScreen
是 false
,它将 return false
而不是 undefined
,这不是 [=] 的有效类型24=]道具。将其更新为如下内容:
prevArrow: (isLargeScreen ? <PrevArrow /> : undefined),
nextArrow: (isLargeScreen ? <NextArrow /> : undefined)
我正在使用 react-slick-slider,我想要实现的是制作自定义箭头。
所以代码看起来像这样:
const FeedbackSlider = () => {
const [isLargeScreen] = useMediaQuery("(min-width: 1050px)")
const sliderRef = useRef<Slider>()
const prev = () => {
sliderRef.current?.slickPrev()
}
const next = () => {
sliderRef.current?.slickNext()
}
const settings = {
dots: false,
infinite: true,
speed: 500,
slidesToShow: (isLargeScreen ? 2 : 1),
slidesToScroll: 1,
prevArrow: (isLargeScreen && <PrevArrow />),
nextArrow: (isLargeScreen && <NextArrow />)
};
return (
<Box px={['2', '4', '8', '16']} mx='auto' pt='12'>
<Slider ref={sliderRef} {...settings}>
// slider cards...
</Slider>
{!isLargeScreen && (
<Flex alignItems='center' w='5xs' justifyContent='space-between' mx='auto' my='5'>
<PrevArrow onClick={prev} />
<NextArrow onClick={next} />
</Flex>)}
</Box >)
用作类型的 Slider 是从我正在使用的 react-slick 导入的 Slider 组件 我只在 VScode 中收到错误代码在浏览器中正常工作:
No overload matches this call. Overload 1 of 2, '(props: Settings | Readonly): Slider', gave the following error. Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'LegacyRef | undefined'. Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'RefObject'. Types of property 'current' are incompatible. Type 'Slider | undefined' is not assignable to type 'Slider | null'. Type 'undefined' is not assignable to type 'Slider | null'. Overload 2 of 2, '(props: Settings, context: any): Slider', gave the following error. Type 'MutableRefObject<Slider | undefined>' is not assignable to type 'LegacyRef | undefined'.
编辑:
No overload matches this call. Overload 1 of 2, '(props: Settings | Readonly): Slider', gave the following error. Type '{ children: Element[]; dots: boolean; infinite: boolean; speed: number; slidesToShow: number; slidesToScroll: number; prevArrow: false | Element; nextArrow: false | Element; ref: MutableRefObject<...>; }' is not assignable to type 'Readonly'. Types of property 'nextArrow' are incompatible. Type 'false | Element' is not assignable to type 'Element | undefined'. Type 'false' is not assignable to type 'Element | undefined'. Overload 2 of 2, '(props: Settings, context: any): Slider', gave the following error. Type '{ children: Element[]; dots: boolean; infinite: boolean; speed: number; slidesToShow: number; slidesToScroll: number; prevArrow: false | Element; nextArrow: false | Element; ref: MutableRefObject<...>; }' is not assignable to type 'Readonly'. Types of property 'nextArrow' are incompatible. Type 'false | Element' is not assignable to type 'Element | undefined'.
看到这部分错误:
Type 'Slider | undefined' is not assignable to type 'Slider | null'. Type 'undefined' is not assignable to type 'Slider | null'.
如果未传递默认值,useRef
会将 undefined
添加到类型。您可能想像这样传递 null:
// bad:
const sliderRef = useRef<Slider>() // type: Slider | undefined
// good
const sliderRef = useRef<Slider | null>(null) // type: Slider | null
关于您的第二个错误,请看一下最后一部分,尝试推断发生了什么:
Types of property 'nextArrow' are incompatible. Type 'false | Element' is not assignable to type 'Element | undefined'.
似乎 nextArrow
接受 Element
或 undefined
。目前你传递的箭头是这样的:
prevArrow: (isLargeScreen && <PrevArrow />),
nextArrow: (isLargeScreen && <NextArrow />)
请注意,如果 isLargeScreen
是 false
,它将 return false
而不是 undefined
,这不是 [=] 的有效类型24=]道具。将其更新为如下内容:
prevArrow: (isLargeScreen ? <PrevArrow /> : undefined),
nextArrow: (isLargeScreen ? <NextArrow /> : undefined)