【发布时间】:2018-07-19 02:09:35
【问题描述】:
我在 Stackoverflow 社区中遇到过完全相同的问题,但是问题的答案并不能解决我的问题。在尝试了互联网上与此问题相关的几乎所有内容后,我一直遇到此问题。所以请回答这个问题,以便继续我的发展。任何帮助都深表感谢。
book-list.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { selectBook } from '../actions/index.js';
import { bindActionCreators } from 'redux';
class BookList extends Component {
renderList() {
return this.props.books.map((book) => {
return (
<li
key={book.title}
onClick={() => this.props.selectBook(book)}
className="list-group-item">
{book.title}
</li>
);
});
}
render() {
return (
<ul className="list-group col-sm-4">
{this.renderList()}
</ul>
)
}
}
function mapStateToProps(state) {
return {
books: state.books
};
}
//Anythin returned from this function will end up as props
// on the BookList container
const mapDispatchToProps = (dispatch) => {
// whenever selectBook is called, the result should be passed
// to all of our reducers
return bindActionCreators({ selectBook: selectBook }, dispatch);
}
//Promote BookList from a component to a container - it needs to know
//about this new dispatch method, selectBook. Make ot available
//as a prop.
export default connect(mapStateToProps, mapDispatchToProps)(BookList);
index.js - 减速器
import { combineReducers } from 'redux';
import BooksReducer from './reducer_books';
const rootReducer = combineReducers({
books: BooksReducer
});
export default rootReducer;
index.js - 操作
function selectBook(book) {
console.log('A book has been selected', book.title);
}
【问题讨论】:
-
您是否正在从您的操作文件中导出
selectBook?如果不是,则无法导入它。 -
您的操作中缺少导出。 Limenwhat 安迪评论道。那应该可以解决您的问题
-
好的!不敢相信我错过了。
标签: javascript reactjs binding redux react-redux