React没有呈现新的CSS风格(React did not render new css style)

编程入门 行业动态 更新时间:2024-10-28 14:22:41
React没有呈现新的CSS风格(React did not render new css style)

这是我的代码逻辑,当用户单击该框时,我会将状态设置为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; }

This is my code logic, When user click the box , I will set the state active to true and getCssStyle() will return drag-and-resize-box-text clicked and then the background image(T) should disappear

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> ); } }

The dev tools show the background image is delete, but the page is still show the image What's wrong with this??

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; }

最满意答案

.drag-and-resize-box-text.clicked应该有background:none。

而且你的代码可以通过使用更简单

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

.drag-and-resize-box-text.clicked should have background: none.

Also your code could be made much simpler by using

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

更多推荐

本文发布于:2023-08-07 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:风格   CSS   React   style   css

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!