简体   繁体   English

流行的 React 组件库(如 MUI/Bootstrap)如何更改元素的类名?

[英]How do popular React component libraries like MUI/Bootstrap change classNames on elements?

https://jquense.github.io/react-widgets/docs/Multiselect/ https://jquense.github.io/react-widgets/docs/Multiselect/

If you look at the multiselect at this link, and inspect element, when you click into the input you'll see the main div element change classnames from 'rw-popup-container' to 'rw-popup-container rw-slide-transition-exited'.如果您查看此链接的多选并检查元素,当您单击输入时,您会看到主 div 元素将类名从“rw-popup-container”更改为“rw-popup-container rw-slide-transition” -退出'。 The class 'rw-slide-transition-exited' contains display=none in css which makes the dropdown disappear. class 'rw-slide-transition-exited' 在 css 中包含 display=none 这使得下拉列表消失。

This process of adding/subtracting classnames is extremely snappy and common among various React libraries like MUI/React Bootstrap.这种添加/减去类名的过程非常快速,并且在 MUI/React Bootstrap 等各种 React 库中很常见。 You can inspect the source HTML and see they are all doing it.您可以检查源代码 HTML,看看他们都在这样做。 How, exactly, are they doing this?他们究竟是怎么做到的? I've looked through the source JS but I can't figure it out.我已经查看了源 JS,但我无法弄清楚。 It doesn't appear to be jQuery addClass()/removeClass() and they are doing conditional rendering in React (which is laggy from personal experience).它似乎不是 jQuery addClass()/removeClass() 并且他们正在 React 中进行条件渲染(从个人经验来看这是滞后的)。

在此处输入图像描述

As you said, this is pretty commong in React libraries (VueJs and Angular libraries as well).正如您所说,这在 React 库(以及 VueJs 和 Angular 库)中很常见。 All the modern javascript frameworks have a way to conditionally set the styles of a component, and they just refresh that attribute, there's no need to re-render everything.所有现代 javascript 框架都有一种方法可以有条件地设置组件的 styles,它们只是刷新该属性,无需重新渲染所有内容。

Particullary for React, you can unse the "className" proeprty for that, instead of passing an string you can pass a function, and that will dynamically change the classes in the component.特别是对于 React,您可以为此取消“className”属性,而不是传递一个字符串,您可以传递一个 function,这将动态更改组件中的类。

Example:例子:

Using the same example you used, if you go here, you'll see the code for that component.使用您使用的相同示例,如果您在此处输入 go,您将看到该组件的代码。

https://github.com/jquense/react-widgets/blob/f604f9d41652adc29ccd3455bf17997bc001d9ef/packages/react-widgets/src/Multiselect.tsx#L632 https://github.com/jquense/react-widgets/blob/f604f9d41652adc29ccd3455bf17997bc001d9ef/packages/react-widgets/src/Multiselect.tsx#L632

(I marked line 632, because that's were the magic happens) (我标记了第 632 行,因为那是奇迹发生的地方)

className={cn(className, 'rw-multiselect')}

In there you can see that className is getting a function (since it's between curly brackets it will be evaluated instead of just passing the value).在那里你可以看到 className 得到了一个 function(因为它在大括号之间,它将被评估而不是仅仅传递值)。

And if I'm correct, it is using this other library: https://github.com/JedWatson/classnames如果我是对的,它正在使用另一个库: https://github.com/JedWatson/classnames

which allows you to conditionally set classes.这允许您有条件地设置类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM