使用 ForwardedRef 反应 FC

mck*_*vak 6 javascript typescript reactjs

我面临着 ref 问题 - 我需要引用函数组件并将 props 传递给它。到目前为止,我得到了我的父组件和子组件。在我的父组件中,我需要对我的孩子使用 ref 。我需要在父组件中使用projectSectionItem。

家长:

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;
        })}
Run Code Online (Sandbox Code Playgroud)

孩子:

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>
    )
}
Run Code Online (Sandbox Code Playgroud)

Shu*_*tri 5

React.forwardRef如果不使用将引用传递给 HTMLDIVElement 或使用useImperativeHandle钩子来公开某些函数,则无法将引用添加到功能组件

由于您希望在 Project 组件中添加 ref DOM 元素,因此您可以使用 innerRef prop 将 ref 传递给 StyledProject 组件,该组件是一个样式组件

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>
    )
});
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您可以访问父级中的引用,例如projectSectionItem.current