【问题标题】:Why is redux.props.reducer returning undefined?为什么 redux.props.reducer 返回 undefined?
【发布时间】:2018-06-18 04:53:30
【问题描述】:

我有一个来自 actions 文件夹的函数,它使用 axios 获取一个 url 并且已经工作。但它从减速器获取数据,但由于某种原因无法正常工作。 我定义了 mapStateToProps 函数

function mapStateToProps(state) {
    return {
        popular: state.popular.results
    };
}

我使用 redux 连接函数连接它

export default connect(mapStateToProps, actions)(Search);

但是当我尝试打印数据时,它给了我 undefined

switch (this.props.popular){
            case null:
                console.log(this.props.popular);
                return;
            case false:
                console.log(this.props.popular);
                return;
            default:
                console.log(this);
                return;
        }

switch case中默认返回一个包含数据的对象

正如你在 props.popular 中看到的,有一个对象数组, 但是当我尝试在 switch 语句的默认情况下打印 this.props.popular 时,它会变得未定义。为什么当我打印 this 时它显示数组存在,但是当我尝试打印 this.props.popular 时,它似乎无法理解它。我做错了什么?

【问题讨论】:

  • 默认情况下添加return default: return this.props.popular
  • 你是使用连接的搜索组件还是类搜索,你能证明一下

标签: javascript reactjs redux react-redux


【解决方案1】:

你的default case 应该包含一些东西,因为 redux 使用它自己的一些操作来初始化,所以,当你的 dispatch 发生时,这个 reducer 已经返回了 undefined

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多