如何从React访问样式?

如何从React访问样式?,第1张

如何从React访问样式

对于React v <= 15

console.log( ReactDOM.findDOMNode(this.refs.container).style); //React v > 0.14console.log( React.findDOMNode(this.refs.container).style);//React v <= 0.13.3

编辑:

获取特定的样式值

console.log(window.getComputedStyle(ReactDOM.findDOMNode(this.refs.container)).getPropertyValue("border-radius"));// border-radius can be replaced with any other style attributes;

对于React v> = 16

使用回调样式或使用createRef()分配引用。

assignRef = element => {  this.container = element;}getStyle = () => {  const styles = this.container.style;  console.log(styles);  // for getting computed styles  const computed = window.getComputedStyle(this.container).getPropertyValue("border-radius"));// border-radius can be replaced with any other style attributes;  console.log(computed);}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5614961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存