Skip links

React Native State

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

// class component 

export default class App extends React.Component {
  state = {
    myState: 'some state informaiton stored'
  } 

  updateState = () => this.setState({ myState: 'The state is updated by onPress event' })

   render() {
      return (
         <View style = {styles.container}>
            <Text>Open up App.js to start working on your app!</Text>
            <Text>Changes you make will automatically reload.</Text>
            <Text>Shake your phone to open the developer menu.</Text>
            <Text onPress = {this.updateState}>{this.state.myState}</Text>
         </View>
      );
   }
}

const styles = StyleSheet.create({
   container: {
      flex: 1,
      backgroundColor: '#ebebeb',
      alignItems: 'center',
      justifyContent: 'center',
   },
});

Leave a comment