简体   繁体   English

反应:错误:无法将未定义或 null 转换为 object

[英]React: error: Cannot convert undefined or null to object

I was trying to solve this error by myself with the help of previous posts but unsuccessful.我试图在以前的帖子的帮助下自己解决这个错误,但没有成功。 I'm trying to take the "state.ingredients" property(I think), I'm talking about the "sugar" "coffee" "water" etc I'm getting this error:我正在尝试获取“state.ingredients”属性(我认为),我正在谈论“糖”“咖啡”“水”等我收到此错误:

TypeError: Cannot convert undefined or null to object This is the CoffeeBuilder.js the important part of the code: TypeError: Cannot convert undefined or null to object 这是 CoffeeBuilder.js 代码的重要部分:

const ingPrices = {
    sugar: 0,
    coffee: 0.2,
    water: 0,
    milk: 0.2,
};

class CoffeeBuilder extends Component {
    constructor(props) {
        super(props);

        this.state = {
            ingredients: {
                sugar: 0,
                coffee: 0,
                water: 0,
                milk: 0,
            },
            totalPrice: 0,
            purchasing: false,
        };
    }

    addIngHandler = (type) => {
        const newCount = this.state.ingredients[type] + 1;
        console.log("clicked");
        const updatedIng = { ...this.state.ingredients };
        updatedIng[type] = newCount;

        const ING_PRICE = ingPrices[type];
        const oldPrice = this.state.totalPrice;
        this.setState({
            totalPrice: oldPrice + ING_PRICE,
            ingredients: newCount,
        });
    };

    render() {
        return (
            <div>
                <CoffeeSlider />
                <CoffeeControls addIng={this.addIngHandler} removeIng={this.removeIngHandler} />
                <OrderSummary ingredients={this.state.ingredients} price={this.state.totalPrice} />
            </div>
        );
    }
}

export default CoffeeBuilder;



This is the OrderSummary.js code:这是 OrderSummary.js 代码:

import React from "react";
import "./OrderSummary.css";

const OrderSummary = (props) => {
    return Object.keys(props.ingredients).map((igKey) => {
        console.log(igKey);
        return (
            <div>
                <li key={igKey}>
                    <span style={{ textTransform: "capitalize" }}>{igKey}</span>:
                    {props.ingredients[igKey]}
                </li>
            </div>
        );
    });
};

export default OrderSummary;

the console.log fetches the data fine, but when I'm trying to return it in a span as a list, it goes wrong. console.log 可以很好地获取数据,但是当我尝试将其作为列表返回时,它出错了。

Please try CoffeBuilder component like this, your code has few issues such as state is not being declared in constructor, no render function.请尝试像这样的 CoffeBuilder 组件,您的代码很少有问题,例如 state 没有在构造函数中声明,没有渲染 function。

Below is the codesandbox link:下面是codeandbox链接:

https://codesandbox.io/s/frosty-mountain-235yk?file=/src/App.js https://codesandbox.io/s/frosty-mountain-235yk?file=/src/App.js

 class CoffeeBuilder extends Component { constructor(props) { super(props); this.state = { ingredients: { sugar: 0, coffee: 0, water: 0, milk: 0, }, totalPrice: 0, purchasing: false, } } render() { return ( < OrderSummary ingredients = { this.state.ingredients } />); } }

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

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