React Native gives us 2 types of button designing technique, First one is its own Button component, Second is Custom Button using TouchableOpacity. So in this tutorial we would going to Change custom TouchableOpacity Button Upper Title Text programmatically using State.
What we are doing in this tutorial :
We are creating 2 buttons in our tutorial, First is Rounded corners button using TouchableOpacity and second is simple button. Now we would set the TouchableOpacity Button Above Title Text using State created in constructor(). We would also create a function to update – change button title state value.
Contents in this project Change Button Upper Title Text Using State:
1. Import StyleSheet, View, Button, Text and TouchableOpacity component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Button, Text, TouchableOpacity } from ‘react-native’;
|
2. Create constructor() in your class with a State named as ButtonText. This state is used as Button Title Text.
1
2
3
4
5
6
7
8
9
10
11
|
constructor(){
super();
this.state={
// This is our Default Button Above Text.
ButtonText : ‘Default Button Title’
}
}
|
3. Create a function named as ChangeButtonTitle(). In this function we would dynamically change the ButtonText State value.
1
2
3
4
5
6
7
8
9
|
ChangeButtonTitle=()=>
{
this.setState({
ButtonText : ‘New Button Title’
})
}
|
4. Create a Root View in render’s return block, Now create a Custom button using TouchableOpacity. Inside the TouchableOpacity component we would set a Text component, Now set ButtonText State in Text component. This would be our Main Button which Text we would change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
render() {
return (
<View style={styles.MainContainer} >
<TouchableOpacity style={styles.ButtonStyle} activeOpacity = { .5 }>
<Text style={styles.TextStyle}> {this.state.ButtonText} </Text>
</TouchableOpacity>
</View>
);
}
|
4. Add a button and call the ChangeButtonTitle() function on button onPress event.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
render() {
return (
<View style={styles.MainContainer} >
<TouchableOpacity style={styles.ButtonStyle} activeOpacity = { .5 }>
<Text style={styles.TextStyle}> {this.state.ButtonText} </Text>
</TouchableOpacity>
<Button title=“Change Above Button Text” onPress={this.ChangeButtonTitle} />
</View>
);
}
|
5. Create Style Classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
const styles = StyleSheet.create(
{
MainContainer: {
flex: 1,
justifyContent: ‘center’,
margin: 20
},
ButtonStyle: {
paddingTop:10,
paddingBottom:10,
backgroundColor:‘#009688’,
borderRadius:5,
marginBottom: 20
},
TextStyle:{
color:‘#fff’,
textAlign:‘center’,
}
});
|
6. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import React, { Component } from ‘react’;
import { StyleSheet, View, Button, Text, TouchableOpacity } from ‘react-native’;
export default class MyProject extends Component {
ChangeButtonTitle=()=>
{
this.setState({
ButtonText : ‘New Button Title’
})
}
render() {
return (
<View style={styles.MainContainer} >
<TouchableOpacity style={styles.ButtonStyle} activeOpacity = { .5 }>
<Text style={styles.TextStyle}> {this.state.ButtonText} </Text>
</TouchableOpacity>
<Button title=“Change Above Button Text” onPress={this.ChangeButtonTitle} />
</View>
);
}
}
const styles = StyleSheet.create(
{
MainContainer: {
flex: 1,
justifyContent: ‘center’,
margin: 20
},
ButtonStyle: {
paddingTop:10,
paddingBottom:10,
backgroundColor:‘#009688’,
borderRadius:5,
marginBottom: 20
},
TextStyle:{
color:‘#fff’,
textAlign:‘center’,
}
});
|
Screenshot in Android device :