HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Interactive UI elements have more than one display state. For example, the modal window may be open or closed, and the switch may be on or off. Generally, you change these states using classes.</p>
1 <p>Interactive UI elements have more than one display state. For example, the modal window may be open or closed, and the switch may be on or off. Generally, you change these states using classes.</p>
2 <p>When you work directly with the DOM, you can use<em>classList</em>, which contains convenient methods for adding and removing classes. React doesn't offer many conveniences out of the box. The<em>className</em>property is just a string, and strings are awkward to process:</p>
2 <p>When you work directly with the DOM, you can use<em>classList</em>, which contains convenient methods for adding and removing classes. React doesn't offer many conveniences out of the box. The<em>className</em>property is just a string, and strings are awkward to process:</p>
3 <p>React's creators suggest resolving this issue using the<em>classnames</em>package, which is easy to use. The approach is straightforward: we generate an appropriate object instead of manipulating a string directly.</p>
3 <p>React's creators suggest resolving this issue using the<em>classnames</em>package, which is easy to use. The approach is straightforward: we generate an appropriate object instead of manipulating a string directly.</p>
4 <p>Then we convert this object into a string:</p>
4 <p>Then we convert this object into a string:</p>
5 <p>Let's substitute specific values:</p>
5 <p>Let's substitute specific values:</p>
6 <p>We can use the cn() function to accept any number of arguments as input.</p>
6 <p>We can use the cn() function to accept any number of arguments as input.</p>
7 <p>If the argument is a string, it's considered a required class. If it's an object, then the logic described above will work:</p>
7 <p>If the argument is a string, it's considered a required class. If it's an object, then the logic described above will work:</p>
8 <p>We can also set mandatory classes in the object:</p>
8 <p>We can also set mandatory classes in the object:</p>
9 <p>Sometimes, we generate the class name dynamically. In that case, we can use the following code:</p>
9 <p>Sometimes, we generate the class name dynamically. In that case, we can use the following code:</p>
10  
10