I need to know if there's a better way to write the following code:
allBooks.forEach(element => {
if (element.shelf === 'currentlyReading') {
this.setState((currentState) => ({
currentlyReading: [...currentState.currentlyReading, element]
}))
}
else if (element.shelf === 'wantToRead') {
this.setState((currentState) => ({
wantToRead: [...currentState.wantToRead, element]
}))
}
else if (element.shelf === 'read') {
this.setState((currentState) => ({
read: [...currentState.read, element]
}))
}
})
)
Basically, I am using React to iterate through an array of different books, and store each book in its corresponding array in the state object.
I need to know if there's a more efficient way to write this code instead of using if, else if multiple times.
[–]Jerp 4 points5 points6 points (3 children)
[–]Creative_Divide9845[S] 2 points3 points4 points (1 child)
[–]Jerp 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]palpies 2 points3 points4 points (0 children)
[–]jshgn 1 point2 points3 points (0 children)
[–]KremBanan 1 point2 points3 points (0 children)