Answer the question
In order to leave comments, you need to log in
How to execute a child element method in React?
Let's say there is a parent component App
, inside of which there are several RandomString
and RefreshButton
When you click on RefreshButton
me from App
, you need to send a command to RandomString
, so that each such component executes its internal methodRandomString.refresh()
export class App extends Component<{}> {
constructor(props: {}) {/* ... */}
protected refreshItems() {
// Тут надо как-то вызвать метод refresh() у дочерних RandomString
}
render(): ReactNode {
return (
<div>
<RandomString />
<RandomString />
<RandomString />
<RefreshButton onClick={this.refreshItems} />
</div>
);
}
}
Answer the question
In order to leave comments, you need to log in
export class App extends Component<{}> {
this.myRef = React.createRef();
constructor(props: {}) {/* ... */}
protected refreshItems() {
// Тут надо как-то вызвать метод refresh() у дочерних RandomString
if(this.myRef&&this.myRef.current&&this.myRef.current.refresh){
this.myRef.current. refresh()
}
}
render(): ReactNode {
return (
<div>
<RandomString this.myRef = React.createRef(); />
<RefreshButton onClick={this.refreshItems} />
</div>
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question