【问题标题】:Uncaught TypeError: _this2.props.selectBook is not a function, automatic duplication of thisUncaught TypeError: _this2.props.selectBook 不是函数,自动复制这个
【发布时间】: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


【解决方案1】:

在这种情况下(和我的)改变这个:

function selectBook(book) {
  console.log('A book has been selected', book.title);
}

到:

export function selectBook(book) {
  console.log('A book has been selected:', book.title);
}

是什么解决了它。就像问题上的 cmets 一样,我没有导出 selectBook。我不完全确定该怎么做。但是这是正确的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2017-09-08
    • 2020-04-29
    • 2019-11-06
    • 2017-09-18
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多