I've got a reusable heading component that allows me to pass a tag prop, creating any sort of heading (h1, h2, h3 etc). Here's that component:
heading.tsx
import React, { ReactNode } from 'react';
import s from './Heading.scss';
interface HeadingProps {
tag: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
children: ReactNode;
className?: string;
}
export const Heading = ({ tag, children, className }: HeadingProps) => {
const Tag = ({ ...props }: React.HTMLAttributes<HTMLHeadingElement>) =>
React.createElement(tag, props, children);
return <Tag className={s(s.heading, className)}>{children}</Tag>;
};
However, I'm coming across a use case where I'd like to be able to have a ref, using the useRef() hook, on the Tag, so that I can access the element and animate with GSAP. However, I can't figure out how do this using createElement.
I've tried to do it by adding a ref directly to the Tag component, and adding it to the Tag props like so:
import React, { ReactNode, useRef } from 'react';
import s from './Heading.scss';
interface HeadingProps {
tag: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
children: ReactNode;
className?: string;
}
export const Heading = ({ tag, children, className }: HeadingProps) => {
const headingRef = useRef(null);
const Tag = ({ ...props }: React.HTMLAttributes<HTMLHeadingElement>) =>
React.createElement(tag, props, children, {ref: headingRef});
return <Tag className={s(s.heading, className)} ref={headingRef}>{children}</Tag>;
};
I receive the error Property 'ref' does not exist on type 'IntrinsicAttributes & HTMLAttributes<HTMLHeadingElement>'.
What am I doing wrong, and how can I safely add a ref to the component?
Thanks.