Like the title, how can we change the class directly using native js instead of using the following method in react?
<p className={ this.state.*** ? class1 : class2 }></p>
滿天的星座2017-07-05 11:08:31
<p ref={(p)=>{this.getDom = p}}></p>
...
// 可以在一个事件中通过this.getDom获取当前元素,再通过className设置
this.getDom.className = class1
三叔2017-07-05 11:08:31
Manipulating DOM with native JS is not recommended. If you really need this capability, you can get the reference to the DOM node through ReactDOM’s findDOMNode
method. For example here:
https://facebook.github.io/re...
But this is indeed a potentially risky behavior [opening a safe door]. The operation of directly modifying the DOM through native JS is redundant, unsafe and inconsistent with React ideas. It is recommended to consider the problem to be solved in the form of JSX.
伊谢尔伦2017-07-05 11:08:31
There must be other ways to do it. You can consider the classnames package. The ref used upstairs is also a common method