javascript - How to modify classname in react without using state?
滿天的星座
滿天的星座 2017-07-05 11:06:31
0
3
1076

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>
滿天的星座
滿天的星座

reply all(3)
滿天的星座
<p ref={(p)=>{this.getDom = p}}></p>
...
// 可以在一个事件中通过this.getDom获取当前元素,再通过className设置
this.getDom.className = class1
三叔

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.

伊谢尔伦

There must be other ways to do it. You can consider the classnames package. The ref used upstairs is also a common method

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template