Answer the question
In order to leave comments, you need to log in
How to pass a function to a child component?
Good afternoon, I need to pass a function to the child component, and in the child component call it so that it re-renders the parent component.
Method (what I called a function) to be passed:
async fetchData(link = 'https://rickandmortyapi.com/api/character', name = '') {
link = `${link}/?name=${name}`;
await fetch(link)
.then((res) => res.json())
.then((data) => {
this.setState({ info: data.info, results: data.results });
})
.catch(() => {
const errorMessage = 'Something went wrong, try again';
this.setState({ errorMessage });
});
}
<Search fetchData={this.fetchData} />
submitInput() {
this.props.fetchData('', this.state.val);
}
() => void
Answer the question
In order to leave comments, you need to log in
Такой тип у пропсов: () => void
And this is the mistake. Correct the type to accept your two arguments.
UPD: You will still swear at the type of the return value of the function. fetchData returns a promise, and you have void types. This is how it should work
fetchData: (link: string, name: string) => void;
fetchData: (link: string, name: string) => Promise<Response>;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question