简体   繁体   English

参考 Typescript 中的道具在反应

[英]Ref as prop in Typescript in react

How can I push ref prop to the child component?如何将 ref prop 推送到子组件? I get an error:我收到一个错误:

'{ reference: RefObject<HTMLSelectElement>; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'.
  Property 'reference' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.

I know it is something about React.FC<> and the generic function but I have no idea what type is it, what should I write between <> to work with refs pushed from another component.我知道这是关于 React.FC<> 和通用 function 但我不知道它是什么类型,我应该在 <> 之间写什么来处理从另一个组件推送的引用。

*EDIT: I know I can use forwardRef but the point of this question is how can I pass ref using props. *编辑:我知道我可以使用 forwardRef 但这个问题的重点是如何使用道具传递 ref。

 const typeRef = useRef<HTMLSelectElement>(null)
    const descriptionRef = useRef<HTMLTextAreaElement>(null)

    const onSubmitHandler = (e: React.FormEvent): void => {
        e.preventDefault()
        
    }

    return (
        <React.Fragment>
            <Navbar />
            <Center width="100%" height="95vh">
                <Flex justifyContent="center" alignItems="center" width="50vw">
                    <FormControl textAlign="center" onSubmit={onSubmitHandler}>
                        <Input ref={titleRef} placeholder="Name for your recipe" />
                        <SelectType reference={typeRef} />
                        <Textarea ref={descriptionRef} placeholder="Description" cols={COLS} rows={ROWS} resize="none" />
                        <Button type="submit">Submit</Button>
                    </FormControl>
                </Flex>
            </Center>
        </React.Fragment>
        
    )
}

child:孩子:

import { Select } from "@chakra-ui/react"

const SelectType: React.FC<> = (props) => {
    return (
    <Select ref={props.refProp}>
        <option>Breakfast</option>
        <option>Lunch</option>
        <option>Dinner</option>
        <option>Supper</option>
    </Select>
    )
}

export default SelectType

In order to pass down a ref to a child component the technique called Forwarding Refs could be used which would help in this scenario, see from the documentation:为了将ref传递给子组件,可以使用称为Forwarding Refs的技术,这将在这种情况下有所帮助,请参阅文档:

Ref forwarding is a technique for automatically passing a ref through a component to one of its children. Ref 转发是一种通过组件自动将 ref 传递给它的一个子组件的技术。

Changing from reference to ref to pass down typeRef as:reference更改为ref以将typeRef传递为:

<SelectType ref={typeRef} />

Then finally within the <SelectType /> component using forwardRef as:然后最后在<SelectType />组件中使用forwardRef作为:

const SelectType = React.forwardRef((_, ref) => {
  return (
    <Select ref={ref}>
      <option>Breakfast</option>
      <option>Lunch</option>
      <option>Dinner</option>
      <option>Supper</option>
    </Select>
  )
})

In this way you don't need to add any type for props .这样你就不需要为props添加任何类型。

You simply need to define the prop types for your SelectType component.您只需要为SelectType组件定义道具类型。

const SelectType: React.FC<{ refProp: React.RefObject<HTMLSelectElement> }> = (props) => { ...

TS Playground TS游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM