【发布时间】:2019-07-27 06:58:10
【问题描述】:
我知道这将需要稍后进行重构以将事物分离到它们自己的组件中,但我目前正面临时间紧缩,需要按原样进行连接。我使用 array.map() 从我用于测试目的的 JSON 对象创建卡片元素。我正在尝试在卡 <div> 上使用 onClick 函数将一些可识别信息(例如“offerid”)保存到组件状态中,然后检查状态中的 id 是否与当前卡匹配。如果匹配,我想在 div 上添加 cardActive 作为 className,这样只有特定的卡片会改变颜色。我不知道该怎么做。就像现在一样,无论选择哪张卡片,所有卡片样式都会更新。下面列出了我的 React 组件和相应的 CSS。任何帮助将不胜感激
反应
import React, { Component } from 'react';
import Grid from '@material-ui/core/Grid';
import './Button.css';
class UsersList extends Component {
constructor(){
super();
this.state = {
cardActive: false,
customers:
[
{
"CustomerId": "1",
"LastName": "Doe",
"FirstName": "Jane",
"Address": {
"Address1": "1811 Chestnut Street",
"Address2": null,
"City": "Philadelphia",
"State": "Pennsylvania",
"Zip": "19103"
},
"Offers": [
{
"OfferId": "Offer1",
"Name": "Offer 1",
"Products": [
{
"ProductId": 1,
"ProductName": "Stuff"
},
{
"ProductId": 2,
"ProductName": "More stuff"
}
],
"Price": "$1"
},
{
"OfferId": "Offer2",
"Name": "Offer 2",
"Price": "$2",
"Products": [
{
"ProductId": 3,
"ProductName": "A lot of stuff"
},
{
"ProductId": 4,
"ProductName": "And then there was stuff"
}
]
},
{
"OfferId": "Offer3",
"Name": "Offer 3",
"Price": "$3",
"Products": [
{
"ProductId": 5,
"ProductName": "Good grief would you look at all this stuff"
},
{
"ProductId": 5,
"ProductName": "What a great deal for stuff"
}
]
}
]
}
]
}
}
selectCard(){
this.setState({ cardActive: !this.state.cardActive })
}
render (){
let card_class = this.state.cardActive ? "cardActive" : "card";
return (
<div>
{this.state.customers.map((customer, index) => {
return <div key={index + customer.CustomerId}>
<h2>Customer</h2>
<hr></hr>
<h3 >Name: {customer.LastName}, {customer.FirstName}</h3>
<h3 >Customer ID: {customer.CustomerId}</h3>
<h3 >
Address:
<br></br>
{customer.Address.Address1}
<br></br>
{customer.Address.City}, {customer.Address.State} {customer.Address.Zip}
</h3>
<br></br>
<h2>Available Offers</h2>
<Grid container spacing={24} justify="center">
{customer.Offers.map((Offer,index) => {
return <div key={index + Offer.OfferId} onClick={this.selectCard.bind(this)}>
<Grid item xs={12}>
<div className="card" class={card_class}>
<div className="container">
<h5><b>{Offer.OfferId}</b></h5>
<h2>{Offer.Name}</h2>
{Offer.Products.map((Product, index) => {
return <div key={index + Product.ProductId}>
<p>+ {Product.ProductName}</p>
</div>
})}
<h3>{Offer.Price}</h3>
</div>
</div>
</Grid>
</div>
})}
</Grid>
</div>
})}
<button className="navbuttonSelected">Submit</button>
</div>
)
}
}
export default UsersList
CSS
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
border-radius: 5px; /* 5px rounded corners */
margin-left: 70px;
margin-right: 70px;
margin-bottom: 5%;
cursor: pointer;
}
.cardActive {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.01s;
border-radius: 5px; /* 5px rounded corners */
margin-left: 70px;
margin-right: 70px;
margin-bottom: 5%;
background: #0c72c5 !important;
color: white !important;
cursor: pointer;
}
.cardActive:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
【问题讨论】:
-
如果你根据 state 中的前一个值设置 state,你应该把它作为一个函数调用:
this.setState((state) => ({ cardActive: !state.cardActive }));这样你就不会被 state 设置的顺序搞砸了
标签: javascript css reactjs onclick