Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Components created using styled-components do not seem to show the correct displayName when debugging; instead they show a fallback like styled.div or styled.span.

E.g.

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

This will show styled.div as display name instead of Bar in the React dev tools.

PS: Styled-components suggests a babel plugin that works for CRA apps. But I see no docs on if there is a similar solution for react-typescript-ts projects.

like image 686
m3h Avatar asked Mar 23 '26 02:03

m3h


1 Answers

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.

like image 118
Alex Mckay Avatar answered Mar 25 '26 16:03

Alex Mckay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!