I have a function in my parent component which is sent to the child components as a prop.In one of my child component,I want the same function(which was sent as a prop from the parent component) to be run twice.The first will be run with some arguments and return the value from that particular child component.The second will be to pass the same function(which came from the parent component,not the one executed in this component) to a separate component as a props again.So my function is:
//Function defined in the parent component and sent as props to the child components
handleShelfChange = (bookOnChange, newSehlf) => {
!this.isTheBookNew(bookOnChange) && this.setState(state => {
let newBooks = state.books.map(book =>
{ if (bookOnChange.id === book.id)
{ book.shelf = newSehlf; }
return book;
});
return {
books: newBooks
};
}
);
BooksAPI.update(bookOnChange, newSehlf);
};
I am sending this function to one of the child component as shown below:
<BookSearch
books={this.state.books}
handleShelfChange={this.handleShelfChange}/>
Now, in my BookSearch Component,I need to perform 2 actions:
1) Use handleShelfChange() method and return values from BookSearch Component.
2) Pass handleShelfChange() method to another component as props which will use the same method again and return some values.
So,I am facing some issues in the first point, I am using the function as a callback to this.setState as
this.setState({ books : book_search }, this.check()); // callback function to this.setState
and the callback function is as shown below:
check() {
let parent_books = this.props.books;
let shelf;
console.log(this.state.books)
const book_result = this.state.books.map((book) => {
const parent = parent_books.find(parent => parent.title === book.title );
if(parent) {
console.log(parent);
book.shelf = parent.shelf;
let shelfChange = this.props.handleShelfChange(book,book.shelf) //Call to the
//function sent as props from the parent component
shelf = shelfChange
}
console.log(shelf) // undefined
return [book,shelf];
})
//console.log(book_result);
this.setState({books: book_result})
console.log(this.state.books)
}
So,I need to run the handleShelfChange() function here for all the books that satisfy the if condition and return it from the check method(the callback method). So, I tried to declare a variable outside the if condition and assign the output of the function to that variable(declared outside the if condition) .I also need to return the each book from the map function satisfying the condition.So,I have used an array to return both the values. But as I have mentioned in the comment, console.log(shelf) return an empty array.It shows undefined.What is the correct way to get both the values from the callback function? Can anyone please suggest me a solution?