Last active
February 24, 2018 17:48
-
-
Save alinz/aa0e8ed48841df6ccbd7c5950a93a813 to your computer and use it in GitHub Desktop.
Styled Component in React-Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import * as React from 'react' | |
import { View, Text, ScrollView, StyleSheet } from 'react-native' | |
const styled = (Component: React.ComponentType<any>) => (style: any = {}) => { | |
const container = StyleSheet.create({ container: style }).container | |
return (props: any = {}) => { | |
const { style, ...rest } = props | |
return <Component style={[container, style]} {...rest} /> | |
} | |
} | |
styled.View = styled(View) | |
styled.Text = styled(Text) | |
styled.ScrollView = styled(ScrollView) | |
export default styled | |
/** | |
example of using this | |
const MyView = styled.View({ | |
flex: 1, | |
backgroundColor: 'red' | |
}) | |
<MyView /> | |
or | |
const StyledCustomComponent = styled(MyCustom)({ | |
flex: 1, | |
backgroundColor: 'red' | |
}) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment