当我们使用样式组件时显示随机类? [英] Random classes getting displayed when we use styled components?

查看:33
本文介绍了当我们使用样式组件时显示随机类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 React 中使用样式化组件.每当我在样式化组件中编写样式并且在浏览器中加载应用程序时,我都会在开发人员工具的元素选项卡中获取一些随机类名称.我只想知道幕后发生了什么?

Am using styled components in React. Whenever i write the styles in styled component and if loads the application in the browser am getting some random classes name in the elements tab of developer tools. I just want to know whats happening behind the scene?

const Button = styled.a`
 display: inline-block;
 border-radius: 3px;
 padding: 0.5rem 0;
 margin: 0.5rem 1rem;
 width: 11rem;
 background: transparent;
 color: white;
border: 2px solid white;
`

render(
 <div>
  <Button
  href="https://github.com/styled-components/styled-components"
  target="_blank"
  rel="noopener"
  primary
>
  GitHub
</Button>
<Button as={Link} href="/docs">
  Documentation
</Button>

)

如果我们在开发者工具中检查和检查元素,我可以看到一些随机类显示如下;

if we inspect and check the element in the developer tools , i can able to see some random classes display like as follow;

<a
  href="https://github.com/styled-components/styled-components"
  target="_blank"
  rel="noopener"
  class = "sc-jDwBTQ "
>
  GitHub
</a>

推荐答案

这就是我在 styled-components 常见问题

This was about all I could find in the styled-components FAQ

每个节点实际上有两个类与之相连:一个是静态的组件,这意味着样式组件的每个元素都有这个类.它没有附加任何样式.相反,它习惯于快速确定 DOM 对象属于或要制作的样式组件DevTools 中的细微变化.它也用于组件选择器.静态类可能类似于:.sc-fVOeaW.

Each node actually has two classes connected to it: one is static per component, meaning each element of a styled component has this class. It hasn't any style attached to it. Instead, it's used to quickly identify which styled component a DOM objects belongs to or to make minor changes in the DevTools. It's also used for component selectors. The static class probably will look something like: .sc-fVOeaW.

另一个是动态的,这意味着每个元素都会不同您的样式组件具有不同的道具,基于什么插值结果.它可能看起来像 .fVOeaW(注意缺少sc"前缀.)

The other is dynamic, meaning it will be different for every element of your styled component with different props, based on what the interpolations result in. It will probably look like .fVOeaW (note the lack of "sc" prefix.)

例如,样式化组件

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆