[英]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.div
或styled.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.