Hi Friends 👋,
Welcome To Infinitbility! ❤️
To add two numbers in react native, use the +
addition operator it will add if your value datatype is a number else if your value datatype is string first convert it using parseInt()
and then perform addition operation.
In the following example, we will take the sample numbers, and strings and perform add operation using the +
addition operator.
Adding Two numbers example
1let num1 = 30;2let num2 = 34;34num1 + num2; // 64
Adding Two string numbers example
1let num1 = "30";2let num2 = "34";34parseInt(num1) + parseInt(num2); // 64
Today, I’m going to show you How do i add two numbers in react native, as mentioned above here, I’m going to use the +
addition operator.
Let’s start today’s tutorial how do you add two numbers in react native?
In this example, we will do
- take an example of a number and string number state
- adding two numbers example
- adding two string numbers example
- print the output on the page screen
1import React, { useState, useEffect } from 'react';2import { Text, View, StyleSheet } from 'react-native';34const App = () => {5 const [firstNum, setFirstNum] = useState(24);6 const [secondNum, setSecondNum] = useState(24);78 const [firstStr, setFirstStr] = useState("24");9 const [secondStr, setSecondStr] = useState("24");1011 return (12 <View style={styles.container}>13 <Text style={styles.paragraph} >{`adding two numbers example`}</Text>14 <Text style={styles.paragraph} >{firstNum + secondNum}</Text>15 <Text style={styles.paragraph} >{`adding two string numbers example`}</Text>16 <Text style={styles.paragraph} >{parseInt(firstStr) + parseInt(secondStr)}</Text>17 </View>18 );19}2021const styles = StyleSheet.create({22 container: {23 flex: 1,24 justifyContent: 'center',25 backgroundColor: '#ecf0f1',26 padding: 8,27 },28 paragraph: {29 margin: 24,30 fontSize: 18,31 fontWeight: 'bold',32 textAlign: 'center',33 },34});3536export default App;
In the above react native example, we have taken the sample numbers and string numbers state, and perform adding two numbers example, and adding two string numbers example.

I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.