Emotion: Number in @emotion/native does not work

Created on 11 Oct 2019  路  10Comments  路  Source: emotion-js/emotion

Current behavior:

If I pass a number to styled, it doesn't work. I need to use toString()

styled.View`
  height:  ${{ paddingTop }: { paddingTop: number }) => props.paddingTop.toString()}px;
`

Expected behavior:

styled.View`
  height:  ${{ paddingTop }: { paddingTop: number }) => props.paddingTop}px;
`

Environment information:

  • react version: 16.9.0
  • react-native version: 0.61.2
  • emotion version: 10.0.20

https://snack.expo.io/H1xW-yR_S

React Native bug

Most helpful comment

Ok, so I've debugged this a little bit and it seems that StyleSheet.create outputs an object with numbers as values and currently, we allow for interpolating those values (numbers) - so it's hard to differentiate between those special numbers and regular ones (like in your case).

The native library was developed by @nitin42 , maybe he could take a look at this or at least clarify if this is intended, how he believes it should work etc.

All 10 comments

It shouldn't matter - because JS should automatically stringify this if you really are passing a number there.

Could you prepare repro case? Without such, we'll have to close the issue as not actionable.

Hi @Andarist, please kindly check https://snack.expo.io/H1xW-yR_S

Ok, so I've debugged this a little bit and it seems that StyleSheet.create outputs an object with numbers as values and currently, we allow for interpolating those values (numbers) - so it's hard to differentiate between those special numbers and regular ones (like in your case).

The native library was developed by @nitin42 , maybe he could take a look at this or at least clarify if this is intended, how he believes it should work etc.

@Andarist thanks for looping in!

It's been a while since I last looked at the @emotion/primitives-core implementation so not sure about the issue exactly. Let me have a look at it this week and I'll post an update @imcvampire

Hey @imcvampire

An alternate solution for this will be to use interpolations in this manner:

const StyledText = styled.Text`
  ${props => props.size ? `font-size: ${props.size}px` : ''};
`

In this way, you can skip toString since the resultant value from the interpolation will be the combined string font-size: <your-prop-value>px. This should help.

Both are workarounds though. Could you describe complexity of implementing a "proper" solution? I suspect it would require tracking more information about already parsed~ stuff to differentiate regular numbers from special StyleSheet IDs, right?

Yes, that's what I was thinking. I'll give it a go 馃憤

Hi @nitin42, do you have any update for this problem?

Thank you!

I know it's been a while, so just wondering if there's a workaround for this at the moment. Thanks!

No workaround - somebody from the community has to step up to implement a fix for this as our team do not have resources to handle issues related to the React Native.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnbrett picture johnbrett  路  3Comments

hamlim picture hamlim  路  3Comments

MrFungusEli picture MrFungusEli  路  3Comments

pimmey picture pimmey  路  3Comments

smlmrkhlms picture smlmrkhlms  路  3Comments