简体   繁体   English

对象作为 React 子项无效(找到:object 和键 {totalItems})。 如果您打算渲染一组孩子,请改用数组

[英]Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead

I am trying to update the number on the badge in cart icon in my site and passing the totalItemls={cart.total_items} as a props to navbar but it't throwing me an error that objects are not valid as react chiled.我正在尝试更新我网站中购物车图标中徽章上的数字,并将 totalItemls={cart.total_items} 作为道具传递给导航栏,但它不会向我抛出一个错误,即对象在反应时无效。 I am using commerce.js api and material UI.我正在使用 commerce.js api 和材质 UI。

i have also console logged the total_items and it outputs a number i just want to pass that number in the badge in the navbar我还控制台记录了 total_items 并输出了一个数字,我只想在导航栏中的徽章中传递该数字

App.js应用程序.js

// import React from 'react';
// import Products from './components/Products/Products';


 import { commerce} from './lib/commerce';

import {Products,Navbar} from './components';
import { useState , useEffect} from 'react';


const App=()=> {
    
    const [products,setProducts] = useState([]);
    const [cart,setCart] = useState({});

    const fetchProducts = async ()=>{

        const {data} = await commerce.products.list();

        setProducts(data);
    }

    const fetchCart = async () => {

         
         setCart(await commerce.cart.retrieve())
    }


    const handleAddToCart = async (productId, quantity) => {
        const item=await commerce.cart.add(productId,quantity);
        setCart(item.cart);
    }
    useEffect(()=>{

        fetchProducts();
        fetchCart();
    },[]);


    console.log(cart)
   
    // console.log(products);
    return (
        <div>
            <Navbar totalItems={cart.total_items}/>
            <Products products={products} onAddtoCart={handleAddToCart}/>
        </div>
    );
}

export default App;

Navbar.js导航栏.js

import React from 'react';
import { AppBar, Toolbar, IconButtone,Badge, MenuItem,Menu,Typography, IconButton } from '@material-ui/core';
import {ShoppingCart} from '@material-ui/icons'

import logo from '../../assets/logo.jpg'
import useStyles from './styles'

const Navbar=(totalItems) =>{

    const classes=useStyles();
    return (
        <div>
            <AppBar position="fixed" className={classes.appbar} color="inherit">
                <Toolbar>
                    <Typography variant="h6" className={classes.title} color="inherit">
                        <img src={logo} alt="Commerce.js"  height="25px" className={classes.image}/>
                        E-Commerce
                    </Typography>
                    <div className={classes.grow}/>
                    <div className={classes.button}>
                        <IconButton aria-label="Show Cart items" color="inherit">
                            <Badge badgeContent={totalItems} color="secondary">
                                <ShoppingCart/>
                            </Badge>

                        </IconButton>

                    </div>
                </Toolbar>

            </AppBar>
        </div>
    );
}

export default Navbar;

The prop passed to a react component needs to be an object like this:传递给反应组件的道具需要是 object ,如下所示:

const Navbar=({totalItems}) =>{

Just add curly braces around the totalItems property at Navbar component as previous response suggests.只需在Navbar组件的totalItems属性周围添加花括号,如先前的响应所示。

暂无
暂无

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

相关问题 对象作为 React 子级无效(找到:object 和键 {children})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {value})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {weight})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {_delegate})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {_delegate}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {arr})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead 对象作为React子对象无效(找到:带有键{this}的对象)。 如果要渲染子级集合,请改用数组 - Objects are not valid as a React child (found: object with keys {this}). If you meant to render a collection of children, use an array instead 如何修复对象作为 React 子对象无效(找到:带有键 {} 的对象)。 如果您打算渲染一组子项,请改用数组 - How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子级无效(找到:object 和键 {rank})。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child (found: object with keys {rank}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:object,键为 {})。 如果您打算渲染子集合,请改用数组。 JS - Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead 未捕获的错误:对象作为 React 子项无效(已找到:带键的对象。如果您打算呈现子项集合,请改用数组 - Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM