javascript - 从样式化组件宏重新导出样式化不起作用

标签 javascript reactjs styled-components

我想从我自己的 .ts 文件中导出 styled-components/macro 但它不起作用。

自定义样式.ts

import styled  from 'styled-components/macro'
import * as styledComponents from 'styled-components' 


import { ThemeInterface } from './theme'

const {
  css,
  createGlobalStyle,
  keyframes,
  withTheme,
  ThemeProvider,
} = styledComponents as styledComponents.ThemedStyledComponentsModule<ThemeInterface>


export { css, createGlobalStyle, keyframes, ThemeProvider, withTheme }
export default styled

App.tsx: 当我直接从 import styled from 'styled-components/macro' 导入它时,它提供了预期的调试功能。但是,当我从我的自己的文件 尝试时,出现了问题。预先谢谢你。

我尝试使用 export { css, createGlobalStyle, keyframes, ThemeProvider, withTheme, styled }。但没有成功。

import React from 'react';
import styled from './custom-styled'

const StyledContainer = styled.div`
background: black;
`
const StyledSpan = styled.span`
  color: white;
  font-size: 20px;
`

function App() {
    return (
        <StyledContainer>
            <StyledSpan>
                Test
            </StyledSpan>
        </StyledContainer>
    );
}

export default App;

预期结果应该是: enter image description here

最佳答案

Edit dazziling-code

如果您使用的是 typescript,则必须导入 @types/styled-components:

npm i @types/styled-components

还需要babel-plugin-styled-components:

npm install --save-dev babel-plugin-styled-components

并在根目录下创建一个.babelrc配置文件。

.babelrc

{
  "plugins": [
    [
      "babel-plugin-styled-components",
      {
        "ssr": true,
        "minify": true,
        "transpileTemplateLiterals": false,
        "pure": false,
        "displayName": false, // generate another classname
        "fileName": false, // generate another classname
        "preprocess": false
      }
    ]
  ]
}

应用.tsx

import styled, {
  Thing,
  GlobalStyle,
  StyledContainer,
  StyledSpan
} from "./custom-styled";
import Reusable from "./Reusable";

const StyledContainer2 = styled(StyledContainer)``;
const StyledSpan2 = styled(StyledSpan)``;
const StyledSpan3 = styled(StyledSpan)`
  color: orange;
  font-weight: bold;
  font-size: 1.5em;
`;

function App() {
  return (
    <StyledContainer2>
      <GlobalStyle />
      <Thing>Thing from suctom-styled</Thing>
      <StyledSpan2>StyledSpan</StyledSpan2>
      <StyledSpan3>Extend StyledSpan</StyledSpan3>
      <Reusable />
    </StyledContainer2>
  );
}

export default App;

自定义样式.ts

import styled, {
  css,
  createGlobalStyle,
  keyframes,
  ThemeProvider
} from "styled-components/macro";

export const GlobalStyle = createGlobalStyle`
  body {
    overflow: hidden;
    background-color: lightcoral;
  }
`;
export const Thing = styled.div`
  color: red;
`;

const move = keyframes`
  50%{ transform: translateX(100px)}
`;

export const StyledContainer = styled.div`
  display: flex;
  flex-flow: column;
  background: black;
`;
export const StyledSpan = styled.span`
  color: white;
  font-size: 20px;
  animation: ${move} 2s linear infinite;
`;

export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled;

可重用.tsx

import { Thing, StyledContainer, StyledSpan } from "./custom-styled";

function Reusable() {
  return (
    <StyledContainer>
      <Thing>Reusable Thing</Thing>
      <StyledSpan>Reusable StyledSpan</StyledSpan>
    </StyledContainer>
  );
}

export default Reusable;

本地 DOM 检查器

enter image description here

本地 React DevTool

enter image description here

关于javascript - 从样式化组件宏重新导出样式化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69547948/

相关文章:

Javascript 正则表达式多重匹配

styled-components - 如何聚焦样式化组件?

reactjs - 为什么 styled-components 中的反引号可以访问 props.theme?

javascript - 跟踪聊天系统中的消息

javascript - 语义 Ui Accordion 获取元素数量

javascript - 似乎无法为特定情况制作正则表达式

javascript - 如何在 React 渲染中优化 HTML 标记

javascript - Action 必须是普通对象。使用自定义中间件进行异步操作 - React Native Redux

javascript - 将图像作为 Prop 动态传递给子组件

javascript - 如何在 React Native 中使用 Tamagui 添加全局样式