Всем привет. Пишу СПА.
Компонент который связан с Редаксом отдает данные через диспатч, но не получает их.
сам компонент:
lass BookList extends Component {
componentDidMount() {
// 1. receive data
const {bookstoreService} = this.props;
const data = bookstoreService.getBooks();
//console.log(data); // full
// 2. dispatch action to store
this.props.booksLoaded(data);
}
render() {
const {books} = this.props;
return (
<ul>
{books.map(
(book) => {
return (<li key={book.id}><BookListItem book={book}/></li>)
})
}
</ul>
)
}
}
const mapStateToProps = ({books}) => {
// console.log(books); // empty
return {books}
};
const mapDispatchToProps ={
booksLoaded
};
export default withBookstoreService()(connect(mapStateToProps, mapDispatchToProps)(BookList));
Редьюсер:
const initialState = {
books: []
};
const reducer = (state = initialState, action) => {
//console.log(action.payload); //full
switch (action.type) {
case 'BOOK_LOADED':
return {
books: action.payload
};
default:
return state;
}
};
export default reducer;
Экшн:
const booksLoaded = (newBooks) => {
// console.log(newBooks); //full
return {
type: 'BOOKS_LOADED',
payload: newBooks
}
};
export {booksLoaded};