Show list of items in <li> tag | EDIT CODE FOR: App.js

PHOTO EMBED

Sat Jun 22 2024 19:34:26 GMT+0000 (Coordinated Universal Time)

Saved by @Black_Shadow #react.js #+jsx

import "./App.css";

function MyButton() {
  return <button>MyButton</button>;
}

const products = [
  { title: "Cabbage", isFruit: false, id: 0 },
  { title: "Garlic", isFruit: false, id: 1 },
  { title: "Apple", isFruit: true, id: 2 },
];

function App() {
  const listitems = products.map((product) => (
    <li style={{ color: product.isFruit ? "red" : "green" }} key={product.id}>
      {product.title}
    </li>
  ));

  return (
    <div>
      <h1>Home</h1>
      <MyButton />
      <ul>{listitems}</ul>
    </div>
  );
}

export default App;
content_copyCOPY

Final Code Overview MyButton Component: Returns a <button> element with the text "MyButton". products Array: Contains three objects, each representing a product with a title, isFruit boolean, and an id. App Component: Maps over the products array to create a list of <li> elements. Each <li> has a style attribute to set the text color based on whether the product is a fruit and uses the product's id as the key. Renders an <h1> element with the text "Home". Renders the MyButton component. Renders the list of products inside an unordered list (<ul>).

From my self