Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert id attribute to styled components created by react-emotion

The basic use of styling plain HTML elements with react-emotion as React components is like this:

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

const StyledContainer = styled.div`
  display: block;
  box-sizing: border-box;
`;

The given code would give us something like this:

<div class="css-gf43fxd ferwf23sd"></div>

Is there anyway I can add an id attibute to that generated HTML element? Thanks in advance.

like image 274
sdabrutas Avatar asked Oct 18 '18 07:10

sdabrutas


People also ask

How do you pass a prop to a styled component?

To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.

Is emotion the same as styled-components?

The uses for Emotion are very different from those of styled-components. The main feature of this library is that the style composition is predictable for writing different CSS styles using JavaScript. This library supports both string and object styles.

Can we use ID in React?

unstable_useOpaqueIdentifier testing reactjs But now, we can use the useId hook to join two elements in React. For example, we can join the label and input tag with the id and for . Every id should be unique on the HTML page.

Can I use variables in styled-components?

In SCSS, you can use variables and formulas for expressing colors, font sizes, widths and spacing variants. However, when you switch from SCSS to styled components, the syntax isn't valid anymore. Styled component files are CSS-in-JS, so they are actually JavaScript files.


1 Answers

Just add the id attribute when you are using this component.

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

const StyledContainer = styled.div`
  display: block;
  box-sizing: border-box;
`;

// and when you want to use this component add id attribute

<StyledContainer id="some-id">...</StyledContainer>
like image 76
Gabriele Petrioli Avatar answered Oct 12 '22 09:10

Gabriele Petrioli