Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props to a styled component in emotion? Using TypeScript

I am using styled by emotion at:

import styled from '@emotion/styled'

I am trying to pass props to a styled component like the guide mentions:

https://emotion.sh/docs/styled

It doesn't work for some reason. I use TypeScript as well. I pass props to my styled component called StyleWrapper here:

const ex: React.FunctionComponent<exProps> = props => {
  return (
    <StyleWrapper someNumber = {props.someNumber}
...
    </StyleWrapper >
  )
}

and in StyleWrapper:

const ToolsHeaderColumn = styled.div`
  padding-top: ${props => props.someNumber };
`

What I get is an error in compilation:

"Property 'someNumber ' does not exist on type 
'Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, 
HTMLDivElement>, "children" | "style" | "title" | 
"color" | "hidden" | "defaultChecked" | "defaultValue" |     "suppressContentEditableWarning" | ... 243 more ... 
| "css"> & { ...; }'.ts(2339)
"
like image 854
Contentop Avatar asked Apr 16 '19 22:04

Contentop


People also ask

Can I pass Props to styled component?

Passed propsIf the styled target is a simple element (e.g. styled. div), styled-components passes through any known HTML attribute to the DOM. If it is a custom React component (e.g. styled(MyComponent)), styled-components passes through all props.

Are emotions faster than styled-components?

emotion performed faster than styled-components back in March 12th when a comparison was done over all CSS-in-JS libraries. However, maintainers of styled-components are actively improving performance and say they are within 0.5-2x emotion's times.

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.

What is shouldForwardProp?

shouldForwardProp ( (prop: string) => bool [optional]): Indicates whether the prop should be forwarded to the Component .

How to use styled-components with props and typescript?

To use styled-components with props and TypeScript, we can create our own interface and use that as the type argument for the styled element function. interface Props { onPress: any; src: any; width: string; height: string; } const Icon = styled.Image<Props>` width: $ { (p) => p.width}; height: $ { (p) => p.height}; `;

How do I use @emotion CSS with typescript?

Emotion includes TypeScript definitions for @emotion/react and @emotion/styled. These definitions infer types for css properties with the object syntax, HTML/SVG tag names, and prop types. The easiest way to use the css prop with TypeScript is with the new JSX transform and the jsxImportSource TSConfig option (available since TS 4.1).

Why is @emotion/react/Babel-plugin required for TypeScript users?

This is because @emotion/react resolves the value of the css prop to a class name and then passes this class name down to the rendered component. @emotion/babel-plugin is completely optional for TypeScript users.

What is styled props in React components?

Passed props If the styled target is a simple element (e.g. styled.div), styled-components passes through any known HTML attribute to the DOM. If it is a custom React component (e.g. styled (MyComponent)), styled-components passes through all props.


1 Answers

by this docs, you can make it like:

type ToolsHeaderProps = {
  someNumber: number
}
const ToolsHeaderColumn = styled('div')`
  padding-top: ${(props: ToolsHeaderProps) => props.someNumber };
`

if you're using typescript 2.9+, you can use this:

const ToolsHeaderColumn = styled.div<ToolsHeaderProps>`
  padding-top: ${(props) => props.someNumber};
`
like image 148
mfakhrusy Avatar answered Oct 25 '22 18:10

mfakhrusy