I want to do API request to the server using data from Reducer (Updated state) after I dispatch it. I would have done it in the Dispatcher if I have enough data, but I only send necessary data to dispatcher.
How should I do it ?
/* Book list container*/
const mapStateToProps = (state) => {
return {
books: state.bookApp.books
}
}
const mapDispatchToProps = (dispatch) => {
// Calling change book title from component
return {
// Dispatch new title to the book list by it's id
onChangeBookTitle: (id, newTitle) => {
dispatch(updateBookTitle(id, newTitle))
}
}
}
// Connect redux to bookList component
export default connect(
mapStateToProps,
mapDispatchToProps
)(BookList);
/* Book list action creator */
export const updateBookTitle = (id, newTitle) => {
return {
type: 'EDIT_BOOK_TITLE',
title: newTitle
}
};
/* Book list reducer */
// Assume there's already a book to update
const defaultState = {
bookList: []
}
const bookApp = (state = defaultState, action) => {
switch(action.type) {
case 'EDIT_BOOK_TITLE' : {
let newBookList;
// Create new copy of booklist
// Do some transformation
// And voilaa... i got new book list
return {...state, bookList: newBookList}
// This is the place where I want to do my API call, using the new BookList data
}
default:
return state;
}