简体   繁体   中英

How to add color to a line-through click event within react?(without it changing the font color too)

All I'm trying to do here, is make it so that the line-through click event turns red upon clicking on it, but I can't seem to find an answer anywhere on how to do this. I tried a few things and nothing has worked. Edit: I added "color":"red" after "none" and now the line is red, but it also turned my font color red.

    <ul
    style={{
      textDecoration: this.props.completed ? "line-through" : "none"
    }}
    onClick={this.handleClick}
    >
    {this.props.task}
    </ul>

You're probably looking for 'the right react syntax for multiple conditional styling' .

Styling requires object then you can try sth like:

<ul
  style={{
    textDecoration: this.props.completed ? "line-through" : "none",
    color: this.props.completed ? "red" : "black"
  }}
  onClick={this.handleClick}
>
{this.props.task}
</ul>

You should read this docs - better solution is to define css class 'task-completed' (red, line-through, italic, whatever) and assign this class conditionally. It can be done (by className) as in docs or a few other ways, fe

<ul className={`task-item ${this.props.completed ? " task-completed" : ""}`}
      onClick={this.handleClick}
>
  {this.props.task}
</ul>

or without ternary operator

className={`task-item ${this.props.completed && "task-completed"}`}

For more complex dependencies you can use classnames

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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