简体   繁体   English

React js:更改项目列表中特定项目的背景颜色

[英]React js : change background color for specific item in list of items

i have my variable messageInbox is array of messages that will display by .map as below我有我的变量 messageInbox 是将通过 .map 显示的消息数组,如下所示

      {messageInbox

            .map((chat ,index)=> (

              <Inbox chat={chat} backgroundColor={this.state.backgroundColor} inBox={this.props.inBox} setInBox={this.props.setInBox} tourists={this.state.tourists.filter(x => x.hotel_id == this.context.hotel_id[0])} chats={this.props.chats} key={index} />

            ))
          }

i want to change the background color of the clicked item by the onClick event in my div我想通过我的 div 中的 onClick 事件更改单击项目的背景颜色


class Inbox extends Component
  constructor(props) {
    super(props);
    this.state = {
      backgroundColor:'#2e405e'
    }

  }





  render() {
    return (
      <div className="InboxContainer" style={{backgroundColor:this.state.backgroundColor}} 
 onClick={ this.setState({backgroundColor:'#111f35'}) } >


        <div className="ImageMsg">
          <img src="/img/chat/contact.png" className="imgContact" />
        </div>

        <div className="TextMsg">
          {this.props.tourists.map(name => {
            return name.id == this.props.chat.sender ?
              <p className="nameContact"> {name.nom}</p>
              : ""
          })}
          {/* <p className="nameContact"> {this.props.chat.sender}</p> */}
          <p className="msgContact">
            {this.props.chat.message}

          </p>
        </div>

        {/* <div className="TimeMsg"> */}
        {/* <p>{this.props.chat.time}</p> */}
        {/* <ReactSVG
              src="/img/chat/notSeen.svg"
              className="svgIconSeensend"
            />
 </div>
       */}




      </div>
    );
  }

}
Inbox.contextType = UserContext
export default withRouter(Inbox);

but she doesn't detect when i clicked on a new item ,can somebody help me,thank's但她没有检测到我何时点击了一个新项目,有人可以帮助我吗,谢谢

I can provide you a simple demo how to do with working example.我可以为您提供一个简单的演示如何使用工作示例。

you can change as per your requirement.您可以根据您的要求进行更改。 Live demo 现场演示

Code代码

class App extends React.Component {
  state = {
    arr: [
      { id: 1, name: "profile", title: "Profile" },
      { id: 2, name: "recruit", title: "Recruitment" },
      { id: 3, name: "arts", title: "Arts" },
      { id: 4, name: "talents", title: "Talents" },
      { id: 5, name: "affection", title: "Affection" }
    ],
    selected: ""
  };
  changeColor = id => {
    this.setState({ selected: id });
  };
  render() {
    const { selected, arr } = this.state;
    return (
      <div>
        <h2>Click to items</h2>
        <ul>
          {arr.map(({ name, id, title }) => (
            <li key={id}>
              <h3 style={{color: selected === id ? "red" : ""}}
                onClick={() => this.changeColor(id)}
                name={name}>
                {title}
              </h3>
            </li>
          ))}
        </ul>
      </div>
    );
  }
}

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

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