styled-components: Passing shorthand margin as a prop throw an error on react-native
Version
1.3.0
Steps to reproduce
Create an element as in the example below and pass a margin prop to it with a string inside.
const Styled = styled.View`
margin: ${props => props.margin};
`;
<Styled margin="50 0" />
As a workaround you can call the prop m
, ex: <Styled m="50 0" />
and it will work fine.
Expected Behavior
Should work as expected as a margin shorthand
Actual Behavior
Throw an error JSON value ‘50 0’ of type NSString cannot be converted to NSNumber
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 21 (10 by maintainers)
I know, but if you pass the props with another name ex
m
instead ofmargin
it will work fine. That’s the thing that bugged me. This example will work fine!It blew my mind to discover that layout props were actually props and not just styles. I came across an issue trying to set position shorthands in my library with an array. I am able to pass shorthands using arrays still for margin and padding like so:
but not for position, because position is type-checked for the enum
["absolute", "relative"] or [0,1]
. But that’s just in my library. Don’t know if that kind of thing is even worth trying here.Opened issue on RN: https://github.com/facebook/react-native/issues/12469
@dbertella I think thats react native related, it doesn’t expect string there, but wants numbers
50 0
in your case you couldmargin={50}
and apply it asmargin-right / margin-left
a bit annoying, asmargin={50 0}
is invalid 😕