How to Use Switch Case Statement in React Native With Example

Switch case statement is used to select one of my choices in all the programming languages. Switch case evaluates the given value or condition and according to them execute the code of block. In react native we can use the switch case statement to match the given value from user and according to them execute the condition or function. Switch case compare the value with each present case, if the matched case found then it will execute its code of block and if none of case matches then it will by default execute the default case. So in this tutorial we would going to make a tutorial on How to Use Switch Case Statement in React Native With Example.

Contents in this project How to Use Switch Case Statement in React Native With Example Tutorial:

1. Import Platform, StyleSheet, View, TextInput, TouchableOpacity, Alert and Text component in your project.

2. Create constructor() in your project and make a State named as TextInput_Data. This State is used to store the TextInput component typed data.

3. Create a function named as checkSwitch() with param parameter. We would call this function on button onPress event and pass a value inside it. Inside this function we would create a Switch case statement with 4 cases and 1 default case. If entered value is matched with any of 4 cases then it will execute their code of block and if none of them found then it will execute the default case.

4. Create 4 functions named as ONE, TWO, THREE and FOUR . We would call these functions on each case as you can see in above code, You can call your own functions here.

5. Creating a TextInput component and 1 Button using TouchableOpacity component.

6. Creating Style.

7. Complete source code for App.js File :

Screenshots:


Use Switch Case Statement in React Native With Example