想要通过ref或者findDOMNode方式获取样式,但是取出来的都是空值
import React from 'react'
import ReactDOM from 'react-dom'
import style from './index.css'
import SplitPanel from '../SplitPanel/index.jsx'
class Main extends React.Component {
componentDidMount () {
const dom = ReactDOM.findDOMNode(this)
console.log('this style:', dom.style)
console.log('input style:', this.textInput.style)
}
render () {
return (
<div className={style.wrapper} >
<input type="text"
ref={(input) => { this.textInput = input }} />
</div>
)
}
}
export default Main
在CodePen中测试
结果:
知道了,
dom.style
得到的是行内样式,要用dom.currentStyle
或者getComputedStyle(dom, null)
取值。