简体   繁体   English

如何在使用样式化组件时在react-scripts-ts项目中显示正确的displayName

[英]How to show correct displayName in react-scripts-ts projects when using Styled Components

I am using react-scripts-ts and styled-components for my React-typescript web app. 我正在为我的React-typescript web应用程序使用react-scripts-ts和styled-components。 Components created using styled-components do not seem to show the correct displayName when debugging; 使用样式化组件创建的组件在调试时似乎没有显示正确的displayName; instead they show a fallback like styled.div or styled.span . 相反,他们显示像styled.divstyled.span的后备。

Eg 例如

const Bar = styled.div`
  background: #ddd;
`;
...
return <Bar />;

This will show styled.div as display name instead of Bar in the React dev tools. 这将在反射开发工具中将styled.div显示为显示名称而不是Bar

PS: Styled-components suggests a babel plugin that works for CRA apps. PS:Styled-components建议一个适用于CRA应用程序的babel插件。 But I see no docs on if there is a similar solution for react-typescript-ts projects. 但是我看到没有关于react-typescript-ts项目是否有类似解决方案的文档。

Instead of importing styled-components like this: 而不是像这样导入样式组件:

import styled from 'styled-components';

Import it like this: 像这样导入:

import styled from 'styled-components/macro';

If that isn't working for you it is likely that you need to update styled-components or react. 如果这对您不起作用,您可能需要更新样式组件或做出反应。 Further information can be found in the styled-components documentation . 更多信息可以在样式组件文档中找到

you can edit your .babelrc to tell it to use displayname 你可以编辑.babelrc告诉它使用displayname

"plugins": [
   ["styled-components", {
      "displayName": true
   }]
]

if you want to do it manually you can write it yourself like so 如果你想手动完成,你可以自己编写

const Bar = styled.div`
  background: #ddd;
`;
...
Bar.displayName = 'Bar'
return <Bar />;

there are packages that you can use that make this easy for you though.. babel-plugin-styled-components for example. 虽然有babel-plugin-styled-components ,但是你可以使用这些软件包让你很容易。 See the styled components docs on tooling 请参阅有关工具的样式化组件文档

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

相关问题 react-scripts-ts 导致构建损坏 - react-scripts-ts results in corrupted build 通过react-scripts-ts进行测试时出现“语法:意外的标识符”错误 - 'Syntax: Unexpected identifier' error when testing via react-scripts-ts &#39;react-scripts-ts&#39; 未被识别为内部或外部命令 - 'react-scripts-ts' is not recognized as an internal or external command 无法读取未定义的属性&#39;thisCompilation&#39; - react-scripts-ts - Cannot read property 'thisCompilation' of undefined - react-scripts-ts 如何使用样式组件扩展 (MUI) React 组件的 TS 接口? - How to extend TS interface for (MUI) React Component using styled-components? Storybook - 如何使用样式组件(React + TS)从外部项目导入组件? - Storybook - How to import a component from a external project using styled-components (React + TS)? react-scripts-ts在所有文件夹中查找依赖项,并在构建时引发错误 - react-scripts-ts looks for dependencies in all folders and throws error on build Node.js 频繁 JavaScript 堆出 memory 在 react-scripts-ts 上崩溃 - Node.js frequent JavaScript heap out of memory crash on react-scripts-ts 使用反应和样式组件打印时如何隐藏按钮? - How to hide a button when printing using react and styled components? 添加测试后,NodeJs JavaScript 堆从 memory 崩溃到 react-scripts-ts - NodeJs JavaScript heap out of memory crash on react-scripts-ts after adding tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM