calculator in react native
app.js
import React, {useState} from 'react';
import {View, Text, StyleSheet, TextInput, Button} from 'react-native';
import Buttoncom from './components/buttons.js';
// const App= props=>{ es6
// function App(){
const App =props=> {
const [result, setresult] = useState(0);
const [num1, setnum1] = useState('');
const [num2, setnum2] = useState('');
return (
<View style={styles.container}>
<TextInput
placeholder="enter first Value"
style={styles.input}
autoCapitalize="none"
keyboardType="number-pad"
autoFocus={true}
value={num1}
onChangeText={val1 => setnum1(val1)}
/>
<TextInput
placeholder="enter second Value"
style={styles.input}
autoCapitalize="none"
keyboardType="number-pad"
autoFocus={false}
value={num2}
onChangeText={val2 => setnum2(val2)}
/>
<View style={styles.btncon}>
<Button
style={{color:'yellow'
}}
title="+"
onPress={() => {
const val1 = parseFloat(num1);
const val2 = parseFloat(num2);
if (val2 === 0) {
setresult(`Aby ${val1} 0 se + nahi hota`);
}else {
setresult(`Sir ji apka answer hai ${val1 + val2}`);
}
}}
/>
<Button
style={{color: 'green'}}
title="-"
onPress={() => {
const val1 = parseFloat(num1);
const val2 = parseFloat(num2);
if (val2 === 0) {
setresult(`Aby ${val1} 0 se - nahi hota`);
} else {
setresult(`'Sir ji apka answer hai' ${val1 - val2}`);
}
}}
/>
<Button
style={{color: 'grey'}}
title="x"
onPress={() => {
const val1 = parseFloat(num1);
const val2 = parseFloat(num2);
setresult(`Sir ji apka answer hai ${val1 * val2}`);
}}
/>
<Button
style={{color: 'brown'}}
title="/"
onPress={() => {
const val1 = parseFloat(num1);
const val2 = parseFloat(num2);
if (val2 == 0) {
setresult('0 se nahi hoga');
} else {
setresult(`Sir ji apka answer hai ${val1 / val2}`);
}
}}
/>
</View>
<Text style={styles.text}>Result: {result}</Text>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
btncon: {
flexDirection: 'row',
width: 350,
justifyContent: 'space-around',
alignItems: 'center',
},
input: {
borderBottomColor: 'green',
borderBottomWidth: 2,
marginVertical: 10,
padding: 5,
fontSize: 33,
},
text: {
fontSize: 45,
textAlign: 'center',
marginTop: 20,
},
});
******************************************************************
components
button.js
import React from 'react';
import {StyleSheet,TouchableOpacity,Text,TouchableHighlight
,TouchableNativeFeedback,TouchableWithoutFeedback} from 'react-native';
const Button = props=>{
return(
<TouchableHighlight onPress={props.onPress}>
<Text style={{ ...styles.Buttonss, ...props.style}}>
{props.title}
</Text>
</TouchableHighlight>
);
}
export default Button;
const styles = StyleSheet.create({
Buttonss:{
fontSize:48,
textAlign:'center',
backgroundColor:'yellow',
padding:15,
}});
******************************************
pkg.json
"dependencies": {
"react": "17.0.1",
"react-native": "0.64.2"
},
Comments
Post a Comment