简体   繁体   English

样式组件是否使用 JSX?

[英]Does a styled-component use JSX?

I am learning to use react and styled components.我正在学习使用 react 和 styled 组件。 While using the styled components package to create a button element, the React import at the top of my code editor gives a warning that react is defined/declared but never used.在使用样式化组件 package 创建按钮元素时,我的代码编辑器顶部的 React 导入会发出警告,指出 react 已定义/声明但从未使用过。

Isn't creating a html button and assigning it to a javascript variable JSX?不是创建 html 按钮并将其分配给 javascript 变量 JSX 吗? Do styled components already have the JSX logic built in?样式化的组件是否已经内置了 JSX 逻辑?

import React from 'react';
import styled from 'styled-components';

const Button = styled.button`
  font: inherit;
  padding: 0.5rem 1.5rem;
  border: 1px solid #8b005d;
  color: white;
  background: #8b005d;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.26);
  cursor: pointer;

&:focus {
  outline: none;
}

&:hover,
&:active {
  background: #ac0e77;
  border-color: #ac0e77;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.26);
}
`

export default Button;

The styled-components library is not a React library. styled-components 库不是 React 库。 It's plain vanilla Javascript (though depends on template literals which are a new feature of JavaScript, so you may need to transpile down to ES5 for broad compatibility).它是普通的 Javascript(尽管取决于模板文字,这是 JavaScript 的新功能,因此您可能需要转译为 ES5 以获得广泛的兼容性)。 In your example you export what is effectively a plain old JavaScript object, not a React component.在您的示例中,您导出的内容实际上是一个普通的旧 JavaScript object,而不是 React 组件。 JSX is a syntax extension for efficiently writing React components in an HTML-like way. JSX 是一种语法扩展,用于以类似 HTML 的方式高效编写 React 组件。 It's not being used here.它没有在这里使用。

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

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