Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

How to change text Value upon Button press in React Native?

$
0
0

I'm an iOS developer currently working on an experimental React Native app.I have the following code which shows a button and sample text on the screen.

import React from 'react';import { StyleSheet, Text, View , Button } from 'react-native';export default class App extends React.Component {  constructor() {    super();    this.state = {sampleText: 'Initial Text'};  }  changeTextValue = () => {    this.setState({sampleText: 'Changed Text'});  }  _onPressButton() {<Text onPress = {this.changeTextValue}>      {this.state.sampleText}</Text>  }  render() {    return (<View style={styles.container}><Text onPress = {this.changeTextValue}>          {this.state.sampleText}</Text><View style={styles.buttonContainer}><Button            onPress={this._onPressButton}            title="Change Text!"            color="#00ced1"          /></View></View>    );  }}const styles = StyleSheet.create({  container: {    flex: 1,    backgroundColor: '#f5deb3',    alignItems: 'center',    justifyContent: 'center',  },  buttonContainer: {}});

The above code displays text and a button.

However when I click the button, the app crashes instead of showing the new text which is to be shown.

I'm new to React Native, kindly guide me on how to solve the error.


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>