To Manage the component data in React Native props and state
Here we are going to learn about state
State
In general, we would initialize state
in the constructor, and will call setState
when we want to change it.
Example
import React from 'react'; import {StyleSheet, Text, View} from 'react-native';
export default class App extends React.Component { state = { text: 'React Native : State Chapter. Here we are going to learn what is React Native State and how to change state of a component.', }; render() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20, }}> <Text style={{ fontSize: 20, }}> {this.state.text} </Text>
<Text style={{ fontSize: 20, margin: 20, padding: 20, }} onPress={() => this.setState({text: 'Chapter Completed '})}> Mark Complete </Text> </View> ); } } |
While running get output as
On Tap on Mark Complete we are changing the state by setState().
We will also learn how to use the arrow function syntax for setState. We should keep in mind that this syntax uses the lexical scope, and this keyword will be bound to the environment object (Class). This will sometimes lead to unexpected behavior
Ruby program to add two integer numbers
how to create an array with Array.[](*args) in Ruby ?
What are the various Ruby runtimes, and how are they different?
Ruby program to check whether the given number is prime or not
Ruby program to reverse a string
Ruby program to check whether the given number is palindrome
Ruby program to print Fibonacci series
How to Replace array elements in Ruby?
Ruby program to print an array
Ruby program to check whether the given number is Armstrong
Program to Print Triangle of Numbers in Ruby
How to add/remove elements to Array in Ruby?
How to shuffle an array in Ruby?
Creating Array with Array.new(size, obj) in Ruby
Ruby program to generate random numbers
Ruby program to Calculate the factorial of given number
What are #method_missing and #send? Why are they useful?
How to Sort Array in Ruby?
How to get index of array element in Ruby
How to Get Input with Gets in Ruby
How to create two dimensional array in ruby?