Refs
import { Component, createRef } from 'react';
class Hinnasto extends Component {
tulosta() {
// ...
}
render() {
// ...
}
}
class Kauppa extends Component {
constructor(props) {
super(props);
this.hinnasto = createRef();
this.textInput = createRef();
}
tulostaHinnasto = () => {
this.hinnasto.current.tulosta();
};
focusTextInput = () => {
this.textInput.current.focus();
};
render() {
return (
<>
<Hinnasto ref={this.hinnasto} />
<button onClick={this.tulostaHinnasto}>Tulosta hinnasto</button>
<input
ref={this.textInput}
type="text"
/>
</>
);
}
}Last updated