A
A
Arthur2019-05-30 22:21:18
React
Arthur, 2019-05-30 22:21:18

Add class?

Good evening.
i can't add class via react setState.

state = {
        done: false
    };
    onLabelClick = () => {
        this.setState({
            done: true
        });
    }
let classNames = "todo-list-item";
       if (done) {
        classNames += " done"
       }

And my css looks like this
.todo-list-item done {
 text-decoration: line-through;
}

In theory, everything with the logic should be correct, I think the error is in the syntax. Please tell me exactly where.
Thank you for your reply)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pashted, 2019-05-30
@TurnerIT

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

and it still makes sense to write like this:
state = {
        done: false,
    };
    
    onLabelClick = () => {
        this.setState({
            done: true
        });
    };

    render() {
        return (
            <div className={'todo-list-item' + (this.state.done ? ' done' : '')} onClick={this.onLabelClick}>text</div>
        );
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question