简体   繁体   English

如何从父组件调用 React/Typescript 子组件 function?

[英]How to call React/Typescript child component function from parent component?

I am trying to use child component function "changeName" in a parent component.我正在尝试在父组件中使用子组件 function “changeName”。 Getting the error Property 'superheroElement' does not exist on type 'App'.获取错误“App”类型上不存在属性“superheroElement”。 What could be the best way to solve this?解决这个问题的最佳方法是什么? The child component and parent component are as follows子组件和父组件如下

import React,{Component} from 'react'; class Superhero extends React.Component { state = { name: "Batman" }; changeName = () => { this.setState({ name: "Bruce Wayne" }); }; render(){ return <div>{this.state.name}</div>; } } export default Superhero; import React, { Component, RefObject } from 'react'; import Superhero from './child' class App extends React.Component<any , any> { constructor(props:any) { super(props); this.superheroElement = React.createRef(); } handleClick = () => { this.superheroElement.current.changeName(); }; render() {return <div><Superhero ref={this.superheroElement}/> <button onClick={this.handleClick}>real name</button></div> } } export default App;

First of all, I don't think this is the best practice for React app as this opposes React's unidirectional data flow.首先,我不认为这是 React 应用程序的最佳实践,因为这与 React 的单向数据流相反。 So, this ref idea should only be the last resort.所以,这个参考想法应该只是最后的手段。

For your problem, the easiest way to prevent the error is set the superheroElement to any.对于您的问题,防止错误的最简单方法是将 superheroElement 设置为 any。

import React from 'react';
import Superhero from './child'
class App extends React.Component<any, any> {

  superheroElement: any;

  constructor(props: any) {
    super(props);
    this.superheroElement = React.createRef();
  }
  handleClick = () => {
    this.superheroElement.current.changeName();
  };
  render() {return 
           <div><Superhero ref={this.superheroElement}/>
          <button onClick={this.handleClick}>real name</button></div>
  }
}
export default App;  

this way, the error will disappear.这样,错误就会消失。 But, if you want to keep the typescript functionality, what you can do is give type to the RefObject.但是,如果您想保留 typescript 功能,您可以为 RefObject 指定类型。

import React, { RefObject } from 'react';
import Superhero from './child';
class App extends React.Component<any, any> {
  superheroElement: RefObject<Superhero>;

  constructor (props: any) {
    super(props);
    this.superheroElement = React.createRef();
  }

  handleClick = () => {
    if (this.superheroElement.current)
      this.superheroElement.current.changeName();
  };

  render () {
    return (
      <div>
        <Superhero ref={this.superheroElement} />
        <button onClick={this.handleClick}>real name</button>
      </div>
    );
  }
}
export default App;

Note: you can give the type of the child to createRef.注意:您可以将孩子的类型赋予 createRef。

this.superheroElement = React.createRef<Superhero>();

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

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