react-native-styled
Installation
yarn add react-native-styled
Usage
Creating a styled component
; const Foo = custom: 'styles' ;<Foo />; // <View style={{ custom: 'styles' }} />
Using primitives
By default, React Native's Text
, TouchableOpacity
and View
are exposed to you, they can be directly used.
styled;styled;styled; // equivalent to custom: 'styles' ; custom: 'styles' ; custom: 'styles' ;
Multiple style objects
const Foo = styled;<Foo />; // <View style={{ custom: 'styles', more: 'styles' .. }} />
Dynamic styles based on props
const Foo = styled;<Foo /> // <View style={{ padding: 0 }} /><Foo padded /> // <View style={{ padding: 10 }} />
Combining static and dynamic styles
const Foo = styled;
Extending styles
const Title = styled;// <Text style={{ fontSize: 20 }} /> const BoldTitle = Title;// <Text style={{ fontSize: 20, fontWeight: 'bold' }} /> const RedBoldTitle = BoldTitle;// <Text style={{ fontSize: 20, fontWeight: 'bold', color: 'red' }} />
Using refs
const List = custom: 'styles' ;<List ref=thislist />; // based on React's forwardRef API (16.3.0)// this.list.scrollTo({ y: 0 })// or this.list.current.scrollTo({ y: 0 }) (with React.createRef)
Defining a custom display name for debugging
;// default names are StyledText, StyledTouchable, StyledView, StyledComponent, etc
Defining propTypes and defaultProps
const StyledComponent = styled;StyledComponentpropTypes = ;StyledComponentdefaultProps = ;