Skip to content

Instantly share code, notes, and snippets.

@dmueller39
Created May 17, 2017 17:26
Show Gist options
  • Save dmueller39/95f24e5bd9012bdf04ce655a68d9d2c5 to your computer and use it in GitHub Desktop.
Save dmueller39/95f24e5bd9012bdf04ce655a68d9d2c5 to your computer and use it in GitHub Desktop.
Basic Working Example for styledComponents
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import { Title, Instructions, ContainerView } from './MainStyledComponents';
export default class styledcomponentsdemo extends Component {
constructor() {
super();
this.state = { sizeClass: 'small' };
}
onLayout(event) {
const sizeClass = event.nativeEvent.layout.width > 500 ? 'large' : 'small';
console.log(sizeClass, event);
this.setState({
sizeClass,
});
}
render() {
const { sizeClass } = this.state;
return (
<ContainerView onLayout={event => this.onLayout(event)}>
<Title sizeClass={sizeClass}>
Welcome to React Native!
</Title>
<Instructions sizeClass={sizeClass}>
To get started, edit index.ios.js
</Instructions>
<Instructions sizeClass={sizeClass}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Instructions>
</ContainerView>
);
}
}
AppRegistry.registerComponent(
'styledcomponentsdemo',
() => styledcomponentsdemo
);
import styled from 'styled-components/native';
const getTitleFontSize = ({sizeClass}) => {
const sizes = {
small: 20,
large: 50,
};
return sizes[sizeClass] || sizes['small'];
};
export const Title = styled.Text`
font-size: ${getTitleFontSize};
text-align: center;
margin: 10;
`;
const getInstructionsFontSize = ({sizeClass}) => {
const sizes = {
small: 14,
large: 24,
};
return sizes[sizeClass] || sizes['small'];
};
export const Instructions = styled.Text`
font-size: ${getInstructionsFontSize};
text-align: center;
color: #333333;
margin-bottom: 5;
`;
export const ContainerView = styled.View`
flex: 1;
justify-content: center;
align-items: center;
background-color: #F5FCFF;
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment