I am trying to use child component function "changeName" in a parent component. Getting the error Property 'superheroElement' does not exist on type 'App'. 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;