简体   繁体   English

ReactJs:使用道具和列表进行样式设计

[英]ReactJs: Styling with props and lists

I am creating a website, and i am using the style in jsx, but, the styling is not working while outputting lists. 我正在创建一个网站,并且正在jsx中使用该样式,但是在输出列表时,该样式无效。

I have done it by outputting each one separately which works, but, I want to make it more reproducible by outputting a list/array. 我已经通过分别输出每个有效的方法来做到这一点,但是,我想通过输出一个列表/数组使其更具可复制性。

There are no error messages about the stying, but I have an error message that it is not recognising a key for each item in the list. 没有有关样式的错误消息,但是我有一条错误消息,它无法识别列表中每个项目的键。

I have created two parts for this products page the first one is were all the products will be where I'll call my second one, which contains the template, then it will render all of them out by reading an the list/array. 我已经为该产品页面创建了两个部分,第一个是所有产品都将被称为第二个产品,其中包含模板,然后它将通过读取列表/数组将所有产品呈现出来。

This is the Main one 'Products.js' 这是主要的“ Products.js”

 import React, { Component } from 'react'; import './Products.css'; import Product from './Product' class Products extends Component { state={ product: [ { name: "T-SHRIT1", colour: "#404040", price: "£50", imgSrc: "./imgs/products/test.png", imgalt: "t-shirt", accentcolour: "#D9D9D9", id: 1 }, { name: "T-SHRIT2", colour: "#404040", price: "£50", imgSrc: "./imgs/products/test.png", imgalt: "t-shirt", accentcolour: "#D9D9D9", id: 3 }, { name: "T-SHRIT3", colour: "#C4C4C4", price: "£50", imgSrc: "./imgs/products/test2.png", imgalt: "t-shirt", accentcolour: "#626262", id: 2 }, { name: "T-SHRIT4", colour: "#C4C4C4", price: "£50", imgSrc: "./imgs/products/test2.png", imgalt: "t-shirt", accentcolour: "#626262", id: 4 }, ] } render() { return ( <div className="Products"> <div className="Products_container"> <Product product={this.state.product} /> </div> </div> ); } } export default Products; 

This is the template code 'Product.js' 这是模板代码“ Product.js”

 import React from 'react'; import './Products.css'; import anime from 'animejs'; const Product = ({product}) => { const Productslist = product.map(item => { return( <div className="Product" style={{background: item.colour,}} key={item.id}> <div className="ProductName" style={{color: item.accentcolour}}>{item.name}</div> <img src={item.imgSrc} alt={item.imgAlt} className="ProductImg"/> <div className="ProductPrice" style={{color: item.accentcolour}}>{item.price}</div> </div> ) }) return ( <div className="Product"> { Productslist } </div> ); } export default Product; 

那里有一个小错字,用item.color替换product.colour

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

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