[react] Property ‘addEventListener‘ does not exist on type ‘never‘

本文探讨了在React Hook模式下遇到'Property 'addEventListener' does not exist on type 'never''错误的情况。通过分析问题,描述了错误产生的原因,并提供了详细的解决方案。

problem

react hook模式,使用useRef,设置click事件监听

const parentRef = useRef(null);
useEffect(()=>{
    parentRef.current?.addEventListener('click',()=>{
        console.log(`parentRef`)
    },false)
}, [])

<div style={parentStyle} ref={parentRef}>
    <div>parent</div>
    <div style={childStyle} ref={childRef}>child</div>
</div>

error

报错: Property ‘addEventListener’ does not exist on type ‘never’

solution

const parentRef = useRef(null);
// 改为
const parentRef = useRef<HTMLInputElement>(null);
To solve the problem that the `disappearTextStyle` property does not exist on the `TextPickerAttribute` type in `TextPicker`, the following steps can be considered: ### Check the Documentation First, refer to the official documentation of the `TextPicker` library. It might contain information about whether the `disappearTextStyle` property is a valid one, or if there are alternative properties to achieve the same functionality. ### Check the Version Ensure that you are using the latest version of the `TextPicker` library. Sometimes, properties are added or removed in different versions. You can update the library to the latest version using the package manager. For example, if you are using npm, you can run the following command: ```bash npm update text-picker ``` ### Extend the Type If the `disappearTextStyle` property is a custom requirement and not part of the official `TextPickerAttribute` type, you can extend the type to include it. Here is an example in TypeScript: ```typescript // Assume this is the original TextPickerAttribute type type OriginalTextPickerAttribute = { // Other existing properties someProperty: string; }; // Extend the type to include the disappearTextStyle property type ExtendedTextPickerAttribute = OriginalTextPickerAttribute & { disappearTextStyle: React.CSSProperties; }; // Now you can use the ExtendedTextPickerAttribute type const pickerAttributes: ExtendedTextPickerAttribute = { someProperty: "value", disappearTextStyle: { color: "red" } }; ``` ### Contact the Library Maintainers If you believe that the `disappearTextStyle` property should be part of the `TextPickerAttribute` type, you can open an issue on the library's GitHub repository. Explain your use case and why you think this property is necessary. The maintainers might consider adding it in future releases.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值