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)
"
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.
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.
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.
shouldForwardProp ( (prop: string) => bool [optional]): Indicates whether the prop should be forwarded to the Component .
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}; `;
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).
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.
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.
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};
`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With