English 中文(简体)
The elements in my <li></li> aren t being displayed on the browser nor in the elements section in the inspect. Only my <ol> tags show
原标题:

The elements in my

  • aren t being displayed on the browser nor in the elements section in the inspect. Only my tags show up in the browser when I inspect the page. Also, the value of total doesn t show up either. However, the items in the birdInCart do exist because when I console.log I can see the items in the array. Why isn t the result from my map function being rendered to the page?

    Here is my code:

    Cart.js file:

    export default function Cart({ birdInCart, total }) {
        return (
            <div className="cart">
                <h2>Shopping Cart</h2>
                <ol>
                    {birdInCart?.map((bird)=> ( 
                        <li key={bird.id}>
                            <p>{bird.name}: {bird.amount}</p>
                        </li>
                    ))}
                </ol>
                <h4>Cart Total: ${total}</h4>
            </div>
        )
    }
    

    App.Js file:

    import { useState } from "react";
    import BirdCard from "./components/BirdCard";
    import Cart from "./components/Cart";
    
    function App() {
      const [birdInCart, setBirdInCart] = useState([]);
      const [total, setTotal] = useState(0);
    
      const addToCart = (bird) => {
        const birdCartItem = {
            id: Math.random(),
            name: bird.name,
            price: bird.amount,
            image: bird.image,
        };
        //setBirdInCart(birdInCart.push(birdCartItem)) - this is not going to work because the push method returns the new length of an array & not the update array. Instead I have to use the spread operator
    
        setBirdInCart([...birdInCart, birdCartItem]);
        setTotal(total + birdCartItem.amount);
        console.log("it works")
        console.log(birdInCart)
      }
    
    
      return (
        <>
          <header>
            <h1>Noni s Bird Sanctuary</h1>
          </header>
          <BirdCard addToCart={addToCart}/>
          <Cart addToCart={addToCart}/>
        </>
      )
    }
    
    export default App;
    

    I tried adding a return and nothing changed. I also console.log the array to check if it is empty. Array is not empty.

  • 最佳回答

    Welcome to stackOverflow so you have applied the ternary operator wrong. If you are trying to iterate over birdInCart array trying doing as following:-

    edited

     {birdInCart?({birdInCart.map((bird)=> ( 
     <li key={bird.id}>
     <p>{bird.name}: {bird.amount}</p> </li> ))}):
     <p>there is nothing to show</p>}
    

    also by the given code add this to cart components props

    <Cart addToCart={addToCart} birdInCart={birdInCart} total=total/>
    

    and use these as props accordingly. Hope it helps.

    问题回答

    暂无回答




    相关问题
    CSS working only in Firefox

    I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

    image changed but appears the same in browser

    I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

    Firefox background image horizontal centering oddity

    I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

    Separator line in ASP.NET

    I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

    热门标签