简体   繁体   English

使用打字稿时如何在props函数中声明react ref

[英]how to declare a react ref in props function when using typescript

I need to pass the refs from one component to a function passed in from another component as a prop, and I need to do it in typescript. 我需要将引用从一个组件传递到从另一个组件传递的函数作为道具,而我需要在打字稿中进行。

So the relevant code inside the child component is 所以子组件内部的相关代码是

class Login extends Component<IProps, IState>  {
    private emailRef = React.createRef<HTMLInputElement>();
    private passRef = React.createRef<HTMLInputElement>();

    constructor(props?) {
        super(props);
    }

    login = () => {
        const email = this.emailRef.current;
        const pass = this.passRef.current;
        this.props.login(email, pass);
    }

which is ok, but now inside of my IProps I should declare login. 没关系,但是现在我应该在IProps内部声明登录名。

And I can't figure out how to do it? 而且我不知道该怎么做?

As an example 举个例子

    interface IProps {
      login: (u: any, p: any) => any,
      errorMessage: ''
    }

obviously those anys are no good, and need to be replaced with something better. 显然,这些都不好,需要用更好的东西代替。 How do I specify that I am in fact going to be passing a react ref here? 我如何指定我实际上将在此处传递react ref?

Declare in constructor() as, constructor()声明为,

 constructor(props?) {
        super(props);
        this.emailRef = React.createRef();
        this.passRef = React.createRef();
    }

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

相关问题 如何在打字稿中声明反应选择的道具? - How to declare props for react-select in typescript? TypeScript,React 导致错误使用 forwardRef 和 props &amp; ref 是 null - TypeScript, React causing errors using forwardRef with props & ref is null 如何将道具传递给 function 并使用 react 和 typescript 在组件中访问它? - How to pass props to a function and access it in a component using react and typescript? Typescript React:如何为 Props 的特定元素声明类型 - Typescript React: How to declare types for specific element which is Props 如何声明 SVG 组件的 props 类型? [React, TypeScript 和 Webpack] - How to declare types of props of SVG component? [React, TypeScript and Webpack] 如何在与打字稿反应时为子类声明不同的状态和道具 - How to declare different state and props for subclasses on react with typescript TypeScript + React:强制使用正确的引用道具 - TypeScript + React: enforce correct ref props 使用钩子将打字稿传递函数作为道具进行反应 - React with typescript passing function as props using hooks 如何使用 React Props 处理 typescript function 过载 - How to handle typescript function overloading with React Props 如何使用带有typescript的useRef分配给ref的函数? - How to assign a function to ref using useRef with typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM