You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Passing shorthand margin as a prop throw an error on react-native #403
@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 :/
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!
On 24 Jan 2017 20:27, "Jacob Parker" ***@***.***> wrote:
I’m not so sure. I think what’s happening here is that the props used
within the component are also passed down to the <View>.
React Native passes styles down as direct props, so <View style={{
margin: 50 }} /> is (unofficially) 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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#403 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACccfOigBQJ4CPD5mX7MrABiPPBxUgCuks5rVl67gaJpZM4LrHy2>
.
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.
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.
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.
@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?
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.
There seem to be only 3 possible solutions at the @moment:
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.
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.
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.
Activity
xzilja commentedon Jan 23, 2017
@dbertella I think thats react native related, it doesn't expect string there, but wants numbers
50 0in your case you couldmargin={50}and apply it asmargin-right / margin-lefta bit annoying, asmargin={50 0}is invalid :/dbertella commentedon Jan 23, 2017
I know, but if you pass the props with another name ex
minstead ofmarginit will work fine.That's the thing that bugged me.
This example will work fine!
mxstbr commentedon Jan 24, 2017
This is a bug in
css-to-react-native, mind submitting an issue there? /cc @styled-components/css-to-react-nativejacobp100 commentedon Jan 24, 2017
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 convertmargin, which it can’t.dbertella commentedon Jan 24, 2017
dbertella commentedon Jan 25, 2017
Apparently it's an hidden feature in
react-nativethis 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.
jacobp100 commentedon Jan 25, 2017
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.
dbertella commentedon Jan 25, 2017
I think I found out what the problem is.
Viewcomponent has this possibleLayout propshttps://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-nativeas max was saying.jacobp100 commentedon Jan 25, 2017
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.
f0rr0 commentedon Feb 20, 2017
Opened issue on RN: react/react-native#12469
f0rr0 commentedon Feb 20, 2017
@mxstbr any updates on this?
f0rr0 commentedon Feb 20, 2017
@jacobp100 Any ideas on possible fixes?
jacobp100 commentedon Feb 20, 2017
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.
f0rr0 commentedon Feb 20, 2017
@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?
jacobp100 commentedon Feb 20, 2017
I'm not sure that addresses the fundamental issue.
ssomnoremac commentedon Feb 20, 2017
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.f0rr0 commentedon Feb 20, 2017
There seem to be only 3 possible solutions at the @moment:
styled-componenetsasking 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 viapropTypesas @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.react-native(e.g.margin,paddingetc.) 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.StyleSheetor inlinestyleand 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.jacobp100 commentedon Feb 23, 2017
I thought of another way of defining properties in effectively the same way as my last comment. It mostly saves typing.
jacobp100 commentedon Mar 1, 2017
Moving discussion to #439
wzup commentedon Jun 6, 2018
@iljadaderko
@jacobp100
RN docs https://facebook.github.io/react-native/docs/layout-props.html#margin
It accepts
string. At least it should.jacobp100 commentedon Jun 6, 2018
Ya it accepts things like
"5%"and"auto"