포착되지 않은 TypeError : _this.props.data.map은 함수가 아닙니다. 구성 요소를 렌더링하기위한 매핑 오류입니다.

펠리페 올라야 오스 피나

여기서 mongo에서 객체 배열을 매핑하려는 구성 요소에서 어떤 일이 발생하는지 모르겠지만 printInventory에 아무것도 표시하지 않지만 this.props.inventory를 호출하면 모든 데이터가 필요합니다! 무슨 일이야?

 printInventory = () => {
    this.props.inventory.map((inventory) => {
        return (
            <CardInventario
                cardTitle={inventory.name}
                quantity={inventory.quantity}
                description={inventory.price}
            />
        )
    })
}

이 기능에서.

다음으로 액션과 리듀서를 보여 드리겠습니다.

inventoryReducer :

import {TAKE_INVENTORY} from '../../types/inventoryTypes';

const INITIAL_STATE ={
    inventory: []
}

function invetoryReducer(state = INITIAL_STATE,action){
    switch (action.type) {
        case TAKE_INVENTORY:
            return {...state, inventory: action.payload}
            break;
        default:
            return state;
            break;
    }
}

export default invetoryReducer;

그리고 여기에 inventoryActions :

import axios from 'axios'
import { TAKE_INVENTORY } from '../../types/inventoryTypes'

export const takeInventory = () => async (dispatch) =>{
    const res = await axios.get('http://localhost:3001/inventory')
        dispatch({
            type: TAKE_INVENTORY,
            payload: res.data
        })
    
}

그리고 전체 구성 요소 :


import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import axios from 'axios'
/* Components and Styles */
import CardInventario from '../components/CardInventario'
import '../assets/styles/container/Stock.scss'
/* Redux */
import * as inventoryActions from '../actions/inventoryActions'

class Stock extends Component {

    componentDidMount() {
        this.props.takeInventory();
    }

    printInventory = () => {
        this.props.inventory.map((inventory) => {
            return (
                <CardInventario
                    cardTitle={inventory.name}
                    quantity={inventory.quantity}
                    description={inventory.price}
                />
            )
        })
    }

    render() {
        console.log(this.props.inventory)
        return (
            <>
                <div className="container">
                    <div className="additem">
                        <Link to="/additem" className="additem__link">
                            <p className="link-add">
                                Añadir Item a Stock
                            </p>
                        </Link>
                    </div>
                    <div className="stock" key="stock">
                        {this.printInventory()}
                    </div>
                </div>
            </>
        )
    }
}

const mapStateToProps = (reducers) => {
    return reducers.inventoryReducer;
}

export default connect(mapStateToProps, inventoryActions)(Stock);

SoftDev

먼저 올바른 매핑을 사용하십시오. inventoryReducer는 매핑하려는 항목이 아닙니다. 그 개체 내부의 인벤토리는 원하는 것입니다.

const mapStateToProps = (reducers) => {
    return reducers.inventoryReducer.inventory;
}

또한 this.props.inventory에서 데이터를 얻으면 중복 키와 관련이 있어야합니다. 다음을 시도하십시오.

printInventory = () => {
    this.props.inventory.map((inventory, index) => {
        return (
            <CardInventario
                key={index}
                cardTitle={inventory.name}
                quantity={inventory.quantity}
                description={inventory.price}
            />
        )
    })
}

아이디가 없으면 대신 인덱스를 사용할 수 있습니다 (권장하지 않음)

printInventory = () => {
    this.props.inventory.map((inventory) => {
        return (
            <CardInventario
                key={inventory.id}
                cardTitle={inventory.name}
                quantity={inventory.quantity}
                description={inventory.price}
            />
        )
    })
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관