How to type forwardRef in separate select component?(如何在单独的SELECT组件中键入ForwardRef?)
问题描述
我使用的技术是Reaction、TypeScrip和Style-Components。 我正在尝试创建选择组件,以便在Reaction Hook表单中使用它。 起初,看起来一切都是正确的,但我从打字脚本中得到了一个错误:
没有重载与此调用匹配。 重载第1个,共2个,‘(道具:Pick<;Pick<;Pick<;DetailedHTMLProps<;SelectHTMLAttributes,HTMLSelectElement&>,&t;Form&...262更多...|&t;在过渡结束捕获&;&;{...;}&;ISelectProps,&t;Form&q;更多...|...264更多...|&q;选项&>&;部分&>|...264更多...|";Options";&;{...;}&;{...;}):ReactElement<;...>;‘,出现以下错误。 类型‘ForwardedRef’不可赋值给类型‘RefObject|(RefObject&;((实例:HTMLSelectElement|NULL)=>;void))|(实例:HTMLSelectElement|NULL)=>;void)&;RefObject<;...>;)|(实例:HTMLSelectElement|NULL)=>;void)&;((实例:HTMLSelectElement|NULL)=>;void))|NULL|未定义’。 无法将类型‘MuableRefObject’赋给类型‘RefObject|(RefObject&;((实例:HTMLSelectElement|NULL)=>;void))|(实例:HTMLSelectElement|NULL)=>;void)&;RefObject<;...>;)|(实例:HTMLSelectElement|NULL)=>;void)&;((实例:HTMLSelectElement|NULL)=>;))|NULL|未定义’。 不能将类型‘MuableRefObject’赋给类型‘((实例:HTMLSelectElement|NULL)=>;void)&;RefObject’。 无法将类型‘MuableRefObject’赋值给类型‘(实例:HTMLSelectElement|NULL)=>;void’。 类型‘MuableRefObject’不提供与签名‘(实例:HTMLSelectElement|NULL):void’匹配的内容。
以下是我的代码:
import React, { forwardRef } from "react";
import styled from "styled-components";
import arrow from "../assets/svg/select_arrow.svg";
interface ISelectProps {
options?: { name: string; value: string | number }[];
name: string;
ref?:
| ((instance: HTMLSelectElement | null) => void)
| React.RefObject<HTMLSelectElement>
| null
| undefined;
}
const SelectContainer = styled.div`
display: flex;
align-items: center;
width: 100%;
position: relative;
`;
const StyledSelect = styled.select<ISelectProps>`
width: 100%;
height: 30px;
padding: 0 10px;
background: ${({ theme }) => theme.colors.transparentButton};
color: white;
border: 1px solid white;
border-radius: ${({ theme }) => theme.borderRadius};
font-size: 14px;
-moz-appearance: none; /* Firefox */
-webkit-appearance: none; /* Safari and Chrome */
appearance: none;
&:focus,
&:active {
outline: none;
}
`;
const StyledArrow = styled.img`
position: absolute;
right: 10px;
padding: inherit;
`;
const Select = forwardRef(({ options, name }: ISelectProps, ref) => {
return (
<>
<SelectContainer>
<StyledSelect name={name} ref={ref}>
{options?.map((op) => (
<option value={op.value}>{op.name}</option>
))}
</StyledSelect>
<StyledArrow src={arrow} />
</SelectContainer>
</>
);
});
export default Select;
codesandbox.io/s/eager-hertz-opbsi?file=/src/select.tsx
我做错了什么? 提前感谢所有有用的答案!
推荐答案
首先,ref
在这种情况下不是道具的一部分。
您应该为forwardRef
提供两个泛型参数以帮助TS缩小类型范围。
ref
?它只是一个HTML元素
import React, { forwardRef } from "react";
import styled from "styled-components";
interface ISelectProps {
options?: { name: string; value: string | number }[];
name: string;
}
const SelectContainer = styled.div`
display: flex;
align-items: center;
width: 100%;
position: relative;
`;
const StyledSelect = styled.select<ISelectProps>`
width: 100%;
height: 30px;
padding: 0 10px;
background: ${({ theme }) => theme.colors.transparentButton};
color: white;
border: 1px solid white;
border-radius: ${({ theme }) => theme.borderRadius};
font-size: 14px;
-moz-appearance: none; /* Firefox */
-webkit-appearance: none; /* Safari and Chrome */
appearance: none;
&:focus,
&:active {
outline: none;
}
`;
const StyledArrow = styled.img`
position: absolute;
right: 10px;
padding: inherit;
`;
const Select = forwardRef<HTMLSelectElement, ISelectProps>(({ options, name }, ref) => {
return (
<>
<SelectContainer>
<StyledSelect name={name} ref={ref}>
{options?.map((op) => (
<option value={op.value}>{op.name}</option>
))}
</StyledSelect>
</SelectContainer>
</>
);
});
export default Select;
Playground link
这篇关于如何在单独的SELECT组件中键入ForwardRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在单独的SELECT组件中键入ForwardRef?


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01