简体   繁体   English

ReactJS - TypeError:requests.map 不是函数

[英]ReactJS - TypeError: requests.map is not a function

I am working on a React application and I am using Redux to store the state.我正在开发一个 React 应用程序,我正在使用 Redux 来存储状态。 I have the following code:我有以下代码:

requests.data.js:请求.data.js:

export default {
    requests: [
        {
            "id": 9,
            "timestamp": Date.now(),
            "description": "Need help with ordering",
            "status": "Completed"
        },
        {
            "id": 2,
            "timestamp": Date.now(),
            "description": "Need help with ordering",
            "status": "Assistance Requested"
        },
        {
            "id": 4,
            "timestamp": Date.now(),
            "description": "Need help with ordering",
            "status": "Assistance Requested"
        }          
    ]
}

requests.reducer.js: requests.reducer.js:

import INITIAL_STATE from './requests.data';
import { CHANGE_REQUEST_STATUS } from './requests.types';

export default (state = INITIAL_STATE, action) => {
    switch (action.type) {
        case CHANGE_REQUEST_STATUS:
            return state;
        default:
            return state;
    }
}

rootReducer.js: rootReducer.js:

import { combineReducers } from 'redux';

import userReducer from './user/user.reducer';
import requestsReducer from './requests/requests.reducer'

export default combineReducers({
    user: userReducer,
    requests: requestsReducer
})

request.component.jsx: request.component.jsx:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import './request.styles.scss';

class Request extends Component {
    render() {
        const { requests } = this.props;
        const requestList = requests.map(request => {
            return (
                <div class="request-box" key={request.id}>
                    <div class="request-details">
                        <div>
                             <h1>Table {request.id}, {request.timestamp}</h1>
                             <h2>{request.description}</h2>
                        </div>
                        <div class="status-button">
                            <button type="button" class="request-button">{request.status}</button>
                        </div>
                    </div>
                </div>
            )
        })
        return (
            <div className="request-list">
                {requestList}
            </div>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        requests: state.requests
    }
}

export default connect(mapStateToProps)(Request);

I am trying to render a list of Request Components, where the request list shows the data that is defined in requests.data.js .我正在尝试呈现请求组件列表,其中请求列表显示requests.data.js定义的数据。

However, I am getting the following error:但是,我收到以下错误:

在此处输入图片说明

I am not sure why I am getting this error and how to resolve it.我不确定为什么会收到此错误以及如何解决它。 Any insights are appreciated.任何见解表示赞赏。

Change this改变这个

const mapStateToProps = (state) => {
    return {
        requests: state.requests
    }
}

to

const mapStateToProps = (state) => {
    return {
        requests: state.requests.requests
    }
}

because you have two reducers and one of them is requests which handles a state that has an array named requests inside it.因为你有两个 reducer,其中一个是requests ,它处理一个状态,里面有一个名为requests的数组。

I would just export an array instead我只想导出一个数组

export default [
            {
                "id": 9,
                "timestamp": Date.now(),
                "description": "Need help with ordering",
                "status": "Completed"
            },
            {
                "id": 2,
                "timestamp": Date.now(),
                "description": "Need help with ordering",
                "status": "Assistance Requested"
            },
            {
                "id": 4,
                "timestamp": Date.now(),
                "description": "Need help with ordering",
                "status": "Assistance Requested"
            }          
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM