/Mobile-CalculatorApp-React-Native

Accelerate mobile app development with a customizable calculator component for React Native. This repository offers a straightforward solution for adding calculation features to your applications.

Primary LanguageJavaScript

  1. Import Statements: javascript import React, { useState } from 'react' import { View, Button, Text, Pressable, ScrollView } from 'react-native' import styles from './MainScreenStyles'

    The code imports necessary modules and components from the react and react-native libraries. It also imports a styles object from a separate file called MainScreenStyles.js.

  2. Functional Component: javascript const MainScreen = () => { // Component logic }

    The code defines a functional component named MainScreen. This component represents the main screen of the calculator.

  3. State Variables: javascript const [value, setValue] = useState('0') const [bracketopen, setBracketOpen] = useState(false)

    The code uses the useState hook to define two state variables: value and bracketopen.

    • The value variable stores the current value of the calculator display. It is initialized with the value '0'.
    • The bracketopen variable keeps track of whether a bracket is currently open or not. It is initialized with the value false.
  4. Event Handler: javascript const handlePress = (val) => { // Event handling logic }

    The code defines an event handler function named handlePress. It takes a parameter val which represents the value of the button pressed on the calculator.

    The event handler contains the logic to handle different button presses and update the calculator display accordingly.

  5. Component JSX: javascript return ( {/* Calculator display */} <ScrollView style={styles.main_screen__display} ref={ref => { this.scrollView = ref }} onContentSizeChange={() => this.scrollView.scrollToEnd({ animated: true })}> {value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

        {/* Calculator keypad */}
        <View style={styles.main_screen__keypad}>
            {/* Keypad rows */}
            {/* ... */}
        </View>
    </View>
    

    )

    The component's JSX code defines the structure and layout of the calculator interface. It consists of a View component that contains a calculator display and keypad.

    • The calculator display is represented by a ScrollView component, which allows scrolling when the display content overflows. The current value of the calculator (value) is rendered inside a Text component. The value is formatted using a regular expression to add commas as thousands separators.

    • The calculator keypad is represented by a View component. It contains multiple rows of buttons, each row represented by a nested View component. The buttons are represented by Pressable components and have associated onPress event handlers that call the handlePress function with the corresponding button value.

  6. Export: javascript export default MainScreen

    The code exports the MainScreen component as the default export, allowing it to be imported and used in other parts of the application.