React native
Styles and positioning More!
We’ll at each of these in much more detail later. See the tutorials at https://facebook.github.io/react-native/docs/style And https://facebook.github.io/react-native/docs/stylesheet
React native Styles and positioning More! Well at each of these in - - PowerPoint PPT Presentation
See the tutorials at https://facebook.github.io/react-native/docs/style And https://facebook.github.io/react-native/docs/stylesheet React native Styles and positioning More! Well at each of these in much more detail later. Styles
Styles and positioning More!
We’ll at each of these in much more detail later. See the tutorials at https://facebook.github.io/react-native/docs/style And https://facebook.github.io/react-native/docs/stylesheet
than background-color.
import { StyleSheet } from 'react-native'; export default homeStyles = StyleSheet.create({ vCenter: { alignItems: 'center', marginTop: 100, }, bigblue: { color: 'blue', fontWeight: 'bold', fontSize: 30, }, red: { color: 'red', }, });
Must import StyleSheet Must export the stylesheet Must must give the stylesheet a name
import React, { Component } from 'react'; import { AppRegistry, Text, View } from 'react-native'; import homeStyles from './styles'; class MyClass extends Component { constructor(props) { super(props); this.state = {isShowingText: true}; } render() { let display = this.state.isShowingText ? this.props.text : ' '; return ( <View> <Text style={homeStyles.red}>{display}</Text> </View> ); } } export default class styleApp extends Component { render() { return ( <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'space- between', alignItems: 'center', marginTop: 200, }}> <MyClass text='Ask not what your country can do for you' /> <MyClass text='To Be or Not To Be' /> </View> ); } }
Class or component name must start with capital Must import the stylesheet. Note the stylesheet is imported by name, e.g., homeStyles. Note that we must indicate the file name and that it is in the same directory as App.js in quotes this is in App.js
import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import Home from './home'; export default class App extends React.Component { render() { return ( <View style={styles.container}> <Home origText = "Ask not what your country can do for you" textStyle={styles.bigblue} /> <Home origText = "To be or not to be" textStyle={styles.red} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, vCenter: { alignItems: 'center', marginTop: 100, }, bigblue: { color: 'blue', fontWeight: 'bold', fontSize: 30, }, red: { color: 'red', }, }); Pass a style that is defined in the file We create 2 instances of the Home class each with different original text and text styles
import React, { Component } from 'react'; import { AppRegistry, Text, View } from 'react-native'; export default class Home extends Component { constructor(props){ super(props); this.state = { myState: this.props.origText, isOrig: true}; } updateState = () => { if (this.state.isOrig){ this.setState({ myState: 'ask what you can do for your country', isOrig: false }) } else{ this.setState({ myState: this.props.origText, isOrig: true }) }; }; render() { return ( <Text style={this.props.textStyle} onPress = {this.updateState}> {this.state.myState} </Text> ); } }
The style was passed as a property. Note that using this.styles.bigBlue would give an error
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export default class FixedDimensionsBasics extends Component { render() { return ( <View> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} /> <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} /> </View> ); } }
each other component with the same parent.
compared to its siblings.
dimensions greater than 0.
will have dimensions of 0 and the flex children will not be visible.
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export default class FlexDimensionsBasics extends Component { render() { return ( // Try removing the `flex: 1` on the parent View. // The parent will not have dimensions, so the children can't expand. // What if you add `height: 300` instead of `flex: 1`? <View style={{flex: 1}}> <View style={{flex: 1, backgroundColor: 'powderblue'}} /> <View style={{flex: 2, backgroundColor: 'skyblue'}} /> <View style={{flex: 3, backgroundColor: 'steelblue'}} /> </View> ); }. }
layout
(column)
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export default class FlexDirectionBasics extends Component { render() { return ( // Try setting `flexDirection` to `column`. <View style={{flex: 1, flexDirection: 'row'}}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} /> <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> </View> ); } };
distribution of children along the primary axis.
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export default class JustifyContentBasics extends Component { render() { return ( // Try setting `justifyContent` to `center`. // Try setting `flexDirection` to `row`. <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'space-between', }}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} /> <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> </View> ); }. };
the alignment of children along the secondary axis
is row, then the secondary is column, and vice versa
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export default class AlignItemsBasics extends Component { render() { return ( // Try setting `alignItems` to 'flex-start' // Try setting `justifyContent` to `flex-end`. // Try setting `flexDirection` to `row`. <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'stretch', }}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{height: 50, backgroundColor: 'skyblue'}} /> <View style={{height: 100, backgroundColor: 'steelblue'}} /> </View> ); }. };
https://facebook.github.io/react-native/docs/layout-props
https://www.tutorialspoint.com/react_native/react_native_flexbox.htm
import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import Home from './Home'; export default class App extends React.Component { render() { return ( <View style={styles.container}> <Home origText = "Ask not what your country can do for you" /> <Home origText = "To be or not to be" /> </View> ); } }
const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, vCenter: { alignItems: 'center', marginTop: 100, }, bigblue: { color: 'blue', fontWeight: 'bold', fontSize: 30, }, red: { color: 'red', }, });
This code is in App.js The main class Create 2 instances of Home Pass different text Note that we want 2 different models, e.g., classes that store different data
import React, { Component } from 'react' import { View } from 'react-native' import PresentationalComponent from './PresentationalComponent' export default class Home extends Component { state = { myState: this.props.origText } render() { return ( <View> <PresentationalComponent myState = {this.state.myState}/> </View> ) } } This code is in Home.js The model or container class Store the passed property in the state Pass the state to the presentation component. Note that we could have just passed the props
import React, { Component } from 'react' import { Text, View, StyleSheet } from 'react-native' export default class PresentationalComponent extends Component{ render() { return ( <Text style = {styles.myState}> {this.props.myState} </Text> ) } } const styles = StyleSheet.create ({ myState: { marginTop: 20, textAlign: 'center', color: 'blue', fontWeight: 'bold', fontSize: 20 } }); This code is in PresentationComponent.js The controller
class The style is created here, not in the model class (e.g., Home) Anytime Home wants to create text with this style, create an instance of this class.