首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试从存储区映射数组,但获取TypeError:*array*.map不是函数

尝试从存储区映射数组,但获取TypeError:*array*.map不是函数
EN

Stack Overflow用户
提问于 2019-06-26 05:52:07
回答 2查看 29关注 0票数 0

我在一个组件中发出异步请求,并将结果分派到存储。结果是对象数组。而不是在其他组件中,我尝试将其映射到标签中。

问题是我得到了类型错误,因为我的reducer的初始状态是null,并且无法映射null。尝试将其更改为仅为空数组,但得到相同的错误。

我的reducer:

const initialState = {
  currentSearchResult: [],
};

const OrgsListReducer = (state = initialState, action) => action.type === 'GET_ORGS_LIST' ? action.payload : state;

有效载荷本身是正常的

我的组件:

class OrgList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoaded: false,
            orgList: this.props.currentSearchResult,
        }
    }

    componentDidMount() {
        if (this.props.currentSearchResult !== []) {
            this.setState({
                isLoaded: true
            })
           }
       }

    render() {
        console.log(this.props.currentSearchResult);
        return this.state.isLoaded ?
            <ul>
                {this.state.orgList.map((org) =>
                    <li key={org.id}>{org.login}</li>
                )}
            </ul>
            :
            <h1>Loading...</h1>
    }
}

function mapStateToProps(state) {
    return {
        currentSearchResult: state.currentSearchResult
    }
}


export default connect(mapStateToProps)(OrgList)
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56762521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档