1

提前感谢您查看此内容。我正在尝试将元素的页面加载类设置为待处理,然后单击 onClick,删除待处理的类并添加已完成的类:

目前,已完成和待处理的状态会相应改变;但是,课程不会改变(待定课程仍处于活动状态,而已完成则不活动)。任何帮助将不胜感激!

import React, { Component } from 'react';
import classNames from 'classnames';
import '../stylesheets/Task.css';

class Task extends Component {
  constructor(props) {
    super(props);

    this.state = {
      completed: false,
      pending: true
    }

    this.makeComplete = this.makeComplete.bind(this);
  }

  makeComplete() {
    if (this.state.pending) {
      this.setState({
        completed: true,
        pending: false
      }, () => {
        console.log('completed: ' + this.state.completed, '\npending: ' + this.state.pending);
      });
    }
  }

  render() {
    return (
      <div className="task">
        <div className="check-task-container" onClick={this.makeComplete} >
          <i className={classNames({
            completed: this.state.completed,
            pending: this.state.pending,
            far: true,
            'fa-circle': true
        })} ></i>
        </div>
        {this.props.title}
      </div>
    );
  }
}
4

2 回答 2

0

如果我理解正确,您需要在 Event onClick 上更改 className。

如果是这样,请在 makeComplete 中更改您的 setState,如下所示:

this.setState({
        completed: !this.state.completed,
        pending: !this.state.pending
      });

或者

this.setState((prevState) => {
  return {completed: !prevState.completed, pending: !prevState.pending};
});

工作演示中的完整代码

于 2018-03-05T05:44:22.957 回答
0

这个问题确实与 i 元素到 svg 的字体转换有关,因为 React 在渲染时不知道元素的变化。为了解决这个问题,我不得不使用 fontawesome 节点模块 ( https://www.npmjs.com/package/@fortawesome/react-fontawesome ) 而不是使用 fontawesome CDN。

于 2018-03-08T02:08:54.113 回答