mckvak
mckvak

Reputation: 411

React FC with forwardedRef

Im facing issue with ref - I need to ref to function component and pass props to it. So far I got my Parent component and Child Component. In my parent component I need to use ref to my child. I need to work with projectSectionItem inside my parent component.

Parent:

const projectSectionItem = useRef<HTMLDivElement>(null);

        return i.showOnHP === 1 ?
            <Project key={index}
                url={`${URL}${i.button_image.image}`}
                client={i.client_name}
                project={i.project.en}
                btn_color={"#000"}
                btn_text={i.button_text}
                href={`/cases/${i.slug}`}
                ref={projectSectionItem}
            >
                {index === 0 ?
                    <ScrollList>
                        {data.map((i, index) => { return <ScrollListItem data-index={index} key={index} ref={projectIndicator} onClick={(e: React.MouseEvent<HTMLLIElement>) => scrollToNextProject(e)} /> })}
                    </ScrollList>
                    : null}
            </Project>
                : null;
        })}

Child:

type APIProps = {
    url?: string,
    client?: string,
    project?: string,
    btn_text?: string,
    btn_color?: string,
    href?: string,
}

type HWWProps = {
    order_client?: number,
    order_project?: number,
    className?: string
    ref?: React.MutableRefObject<HTMLDivElement>
}

type ProjectProps = APIProps & HWWProps;

export const Project: React.FC<ProjectProps> = props => {
    return (
        <StyledProject className={props.className}>
            <ProjectContainer>
                <ProjectImage src={props.url} alt={props.client} />
                <ProjectTextWrapper>
                    <ProjectBrand order_client={props.order_client}>{props.client}</ProjectBrand>
                    <ProjectName order_project={props.order_project}>{props.project}</ProjectName>
                    <ButtonExtend as={Button} color={props.btn_color}><Link to={props.href}>{props.btn_text}</Link></ButtonExtend>
                </ProjectTextWrapper>
            </ProjectContainer>
            {props.children}
        </StyledProject>
    )
}

Upvotes: 6

Views: 16254

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282140

You cannot add refs to functional component without using React.forwardRef to either pass it on to a HTMLDIVElement or using useImperativeHandle hook to expose some functions

Since you wish to add the ref a DOM element in Project component you can pass the ref on to the StyledProject component which is a styled component by using innerRef prop

export const Project: React.FC<ProjectProps> = React.forwardRef((props ,ref: : Ref<HTMLDivElement>)=> {
    return (
        <StyledProject className={props.className} innerRef={ref}>
            <ProjectContainer>
                <ProjectImage src={props.url} alt={props.client} />
                <ProjectTextWrapper>
                    <ProjectBrand order_client={props.order_client}>{props.client}</ProjectBrand>
                    <ProjectName order_project={props.order_project}>{props.project}</ProjectName>
                    <ButtonExtend as={Button} color={props.btn_color}><Link to={props.href}>{props.btn_text}</Link></ButtonExtend>
                </ProjectTextWrapper>
            </ProjectContainer>
            {props.children}
        </StyledProject>
    )
});

After doing this, you can access the ref in parent like projectSectionItem.current

Upvotes: 5

Related Questions