Skip to content

Passing shorthand margin as a prop throw an error on react-native #403

Closed

Description

@dbertella

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

Activity

  1. xzilja commented on Jan 23, 2017

    @xzilja
    Contributor

    @dbertella I think thats react native related, it doesn't expect string there, but wants numbers 50 0 in your case you could margin={50} and apply it as margin-right / margin-left a bit annoying, as margin={50 0} is invalid :/

  2. dbertella commented on Jan 23, 2017

    @dbertella
    Author

    I know, but if you pass the props with another name ex m instead of margin it will work fine.
    That's the thing that bugged me.
    This example will work fine!

    const Styled = styled.View`
        margin: ${props => props.m};
    `;
    
    <Styled m="50 0" />
    
  3. mxstbr commented on Jan 24, 2017

    @mxstbr
    Member

    This is a bug in css-to-react-native, mind submitting an issue there? /cc @styled-components/css-to-react-native

  4. jacobp100 commented on Jan 24, 2017

    @jacobp100
    Contributor

    I’m not so sure. I think what’s happening here is that the props used for styling are also passed down to the <View>.

    React Native (sort of) passes (some) styles down as direct props, so <View style={{ margin: 50 }} /> is (sometimes) the same as <View margin={50} />.

    Effectively, we’re getting <View style={{ marginTop: 50, marginRight: 0, marginBottom: 50, marginLeft: 0 }} margin="50 0" />. React Native is trying to convert margin, which it can’t.

  5. dbertella commented on Jan 24, 2017

    @dbertella
    Author
  6. dbertella commented on Jan 25, 2017

    @dbertella
    Author

    Apparently it's an hidden feature in react-native this morning I tried this <View margin={5} padding={80} borderWidth={10} backgroundColor="red"> and it's working fine as I was adding those styles inlines. I guess it's not a library's related issue then.
    I don't know if the library can handle this situation before to pass the props at the native component.

  7. jacobp100 commented on Jan 25, 2017

    @jacobp100
    Contributor

    It is a library issue.

    There’s currently very limited support distinguish between props used for styling (which should not be passed down to the target component), and props for the target component. The current logic is to filter out all non-DOM props for DOM elements, but it goes no further.

    All other elements will have both the props intended for the component and styling props passed down.

  8. dbertella commented on Jan 25, 2017

    @dbertella
    Author

    I think I found out what the problem is. View component has this possible Layout props https://facebook.github.io/react-native/docs/layout-props.html#props. I'm not sure you can filter them out and if it's what the final user always want though.

    Anyway I think we have to move this issue to css-to-react-native as max was saying.

  9. jacobp100 commented on Jan 25, 2017

    @jacobp100
    Contributor

    The layout props are intended to be used only within in styles. It’s just a quirk of React Native that they’ll sometimes work when used as direct props.

    Regardless of the behaviour of RN, there will be other components this can affect.

  10. f0rr0 commented on Feb 20, 2017

    @f0rr0

    Opened issue on RN: react/react-native#12469

  11. f0rr0 commented on Feb 20, 2017

    @f0rr0

    @mxstbr any updates on this?

  12. f0rr0 commented on Feb 20, 2017

    @f0rr0

    @jacobp100 Any ideas on possible fixes?

  13. jacobp100 commented on Feb 20, 2017

    @jacobp100
    Contributor

    It's hard, because you're ultimately going to have to make a trade-off between developer friendliness and correctness.

    We could have the developer to define propTypes on their component, and then we only pass on the properties outside of the defined propTypes.

    const StyledView = styled(View)`
      margin: ${props => props.margin};
    `
    
    StyledView.propTypes.margin = PropTypes.string
    
    <StyledView margin="10" passedProp={true} />
  14. f0rr0 commented on Feb 20, 2017

    @f0rr0

    @jacobp100 Assuming that the upstream issue is not a bug, shouldn't styled-components match all props with LayoutProps and apply a css-to-react-native transform before passing them to the underlying component?

  15. jacobp100 commented on Feb 20, 2017

    @jacobp100
    Contributor

    I'm not sure that addresses the fundamental issue.

  16. ssomnoremac commented on Feb 20, 2017

    @ssomnoremac

    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:

     <View margin={[2,4]} />
    

    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.

  17. f0rr0 commented on Feb 20, 2017

    @f0rr0

    There seem to be only 3 possible solutions at the @moment:

    1. styled-componenets asking developers to explicitly declare what props their component depends on and not passing those particular props to the underlying component that gets styled. This could be via propTypes as @jacobp100 suggested in Passing shorthand margin as a prop throw an error on react-native #403 (comment). However, I'm not sure how dirty it'll get in case the developer does not want to opt into this selective passing down of props.
    2. Apply transformations to all props that are style props and are type checked by react-native (e.g. margin, padding etc.) before passing them to the underlying component. Implementing this would involve figuring out an exhaustive list of such props that are typechecked and maintaining it as new props are added upstream.
    3. Ditch the StyleSheet or inline style and pass transformed layout props down as direct inline props to the underlying component overriding the props passed to the styled component. This assumes that this weird bahavior is consistent for all layout props.
  18. jacobp100 commented on Feb 23, 2017

    @jacobp100
    Contributor

    I thought of another way of defining properties in effectively the same way as my last comment. It mostly saves typing.

    const DynamicStyles = styled.div`
      @property color, margin;
    
      color: ${props => props.color};
      margin: ${props => props.margin};
    `
  19. jacobp100 commented on Mar 1, 2017

    @jacobp100
    Contributor

    Moving discussion to #439

  20. jacobp100 commented on Jun 6, 2018

    @jacobp100
    Contributor

    Ya it accepts things like "5%" and "auto"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions