A
A
Arthur2019-05-31 19:01:00
css
Arthur, 2019-05-31 19:01:00

Stuck on a test lesson?

Now I'm watching video tutorials on react, parsing state, setState.
In the code below, only one state-changing function works, namely onLabelClick. The onMarkClick function either doesn't work, or works in the same way as onLabelClick, although it is called on another object - on the button.
Below is the React and Css code:

import React from 'react';
import './todo-list-item.css';

class TodoListItem extends React.Component {
    state = {
        done: false,
        important: false
    };

    onLabelClick = () => {
        this.setState({
            done: true
        });
    };
    onMarkClick = () => {
        this.setState({
            important: true
        });
    }
    render () {
       const { label } = this.props;
       const { done, important } = this.state;
       let classNames = "todo-list-item";
       if (done) {
        classNames +=  " done"
       };
       if (important) {
           classNames += " important"
       };
        return (
         <span>
           <span className= {classNames} 
              onClick= {this.onLabelClick} >{ label }
              <button type="button"  onClick= {this.onMarkClick } ><i className="fa fa-exclamation"/></button>
            </span>
        </span> 
        )
    }
}
export default TodoListItem ;

.todo-list-item.done  {
 text-decoration: line-through;
};

.todo-list-item.important  {
    font-weight: bold;
    color: blue;
}
.fa {
    color: green;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-05-31
@TurnerIT

1. You need to either stop the event from bubbling:

onMarkClick = (e) => {
    e.stopPropagation();
    this.setState({
      important: true,
    });
  };

or change the structure:
return (
  <span>
    <span
      className={classNames} 
      onClick={this.onLabelClick}
    >
      {label}
    </span>
    <button type="button"  onClick= {this.onMarkClick}>
      <i className="fa fa-exclamation"/>
    </button>        
  </span> 
);

2.
.todo-list-item.done {
  text-decoration : line-through;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question