【问题标题】:React.JS Context API Problem (Rendering Problem)React.JS 上下文 API 问题(渲染问题)
【发布时间】:2021-04-18 16:38:58
【问题描述】:

我目前正在尝试实施一个电子商务项目。我的问题是关于 Context API 和 Reducer。 无法呈现产品的价格和其他详细信息。在购物车组件中

上下文.Js ; Here is my Context.js

Reducer.JsHere is the Reducer.js

我的产品组件代码是;

import './Product.css'
import AddBoxIcon from '@material-ui/icons/AddBox';
import { Context } from "./Context"
import React, {useContext} from "react"




function Product({title,price,image,id}) {

   const {addtoBasket} = useContext(Context)

/*
  function alertUser() {
        alert(`${title} added to your card`)
    }

    function addBasket(){
        console.log(title,price)
    }


    function firedFunctions(){
        alertUser()
        addBasket()
    
    }

*/
    
    return (
        <div className="product">
            <div className="product__info">
                <img src={image} alt=""></img>
                <p className="title">{title}</p>
                <div className ="price__add" >
                    <h4>Price : {price} $</h4>
                    <div onClick={() => addtoBasket(title, price, image,id)}>
                    <AddBoxIcon className="btn" />
                    </div>
                </div>                
            </div>            
        </div>
    )
}

export default Product

我的购物车组件代码是;

import React, {useContext} from 'react'
import './Cart.css'
import {Context} from "./Context"
import Product from "./Product"


function Cart() {

    const {basket} = useContext(Context)
    console.log(basket) // Expected: Empty Array
    return (
        <div className="cart">
            <div className="cart__left">
                <h2>Your Products</h2>
                    <div>
                        {basket.map((product) =>(                          
                            <h4>{product}</h4>
                            {/*why product give me just a title of product*/}
                                                                     
                        ))}
                        
                    </div>

            </div>
            
            <div className="cart__right">
                <h2>Total</h2>
            </div>
        </div>
    )
}

export default Cart

And this is the output when i click the button v.1(console) ;

I want to see Price and image either.

【问题讨论】:

  • Offtopic - 您粘贴的图片中的字体名称是什么?
  • 运算符 Mono..

标签: javascript reactjs react-context


【解决方案1】:

您将三个参数传递给addToBasket 函数,但它只接收一个对象。

您可能需要的是:

<div onClick={() => addtoBasket({title, price, image, id)}>

我刚刚在codesandbox中复制了它,这是正确的答案,看看: https://codesandbox.io/s/awesome-violet-fr7np?file=/src/index.js

【讨论】:

  • 不,不是为了那个。还是谢谢你
  • 它在这里就像一个魅力,只是编辑了答案
  • 在控制台中我可以看到类似的输出; 0: {title: "Iphone XS Max", price: 3000, image: "cdn.vatanbilgisayar.com/Upload/PRODUCT/apple/thumb/…", id: 2} 但它没有呈现我收到类似的错误:未捕获的错误:对象作为 React 子级无效(发现: 带有键 {title, price, image, id} 的对象)。如果您打算渲染一组子项,请改用数组。
  • 你不能像以前那样只是&lt;h4&gt;{product}&lt;/h4&gt;,看看我创建的沙盒
  • 我看到了.. 非常感谢,感谢您的宝贵时间 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多