react-bits
react-bits copied to clipboard
[BUG]: text-animations/scroll-velocity on TS + Tailwind on ESLint
Describe the issue
ESLint error
Explain what this problem is and help me fix it: Argument of type 'RefObject<HTMLSpanElement | null>' is not assignable to parameter of type 'RefObject<HTMLElement>'.
Type 'HTMLSpanElement | null' is not assignable to type 'HTMLElement'.
Type 'null' is not assignable to type 'HTMLElement'.
The error is because strict types
The fix is to change the useElementWidth
After that everything should be fine
Reproduction Link
No response
Steps to reproduce
- Install ESLint with strict mode
- Run
npm run lint - And there will be an error
to fix that we ened to change useElementWidth
to
function useElementWidth<T extends HTMLElement>(ref: React.RefObject<T | null>): number {
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
function updateWidth() {
if (ref.current) {
setWidth(ref.current.offsetWidth);
}
}
updateWidth();
window.addEventListener("resize", updateWidth);
return () => window.removeEventListener("resize", updateWidth);
}, [ref]);
return width;
}
Validations
- [x] I have checked other issues to see if my issue was already reported or addressed