【问题标题】:React did not render new css styleReact 没有渲染新的 CSS 样式
【发布时间】:2016-09-27 15:33:28
【问题描述】:

这是我的代码逻辑,当用户点击该框时,我会将状态active设置为true
getCssStyle() 将返回drag-and-resize-box-text clicked 然后背景图像(T)应该消失

class Box extends Component {
  constructor(props) {
    super(props);
    this.state = {
      active: false,
    }
  }

  // decide which style to be used
  getCssStyle = (type) => {
    switch (type) {
      case 'text':
        if (this.state.active) {
          console.log("AAA")
          return 'drag-and-resize-box-text clicked'
        } else {
          console.log("BBB")
          return 'drag-and-resize-box-text';
        }
        // break;
      default:
      return '';
    }
  }

  onClick = () => {
    this.setState({active: true});  
  }

  render() {
    return (    
          <div className={this.getCssStyle(boxType)} >
            {this.boxFillContent()}
          </div>
      </div>
    );
  }
}

开发工具显示背景图片已删除,但页面仍显示图片
这有什么问题??

css

.drag-and-resize-box-text{
  border: dashed 3px LightSeaGreen;
  background: url(../images/bottom/text_normal.png)  no-repeat;
  background-position:center;
  width: inherit;
  height: inherit;
}

.drag-and-resize-box-text.clicked{
  border: dashed 3px LightSeaGreen;
  /*background: url(../images/bottom/text_normal.png)  no-repeat;*/
  background-position:center;
  width: inherit;
  height: inherit;
}

【问题讨论】:

    标签: css reactjs


    【解决方案1】:

    .drag-and-resize-box-text.clicked 应该有背景:无。

    你的代码也可以通过使用变得更简单

    <div className={this.state.active ? 'drag-and-resize-box-text clicked' : 'drag-and-resize-box-text'} >
        {this.boxFillContent()}
    </div>
    

    【讨论】:

    • 谢谢! background: none 运行良好,您的代码也更好!