Counter App using React Native
Certainly! Below is a simple documentation for the provided React Native counter app:
React Native Counter App Documentation
Overview
This is a simple React Native application that implements a basic counter functionality. Users can increment or decrement the counter value by pressing the corresponding buttons.
Components
App Component
The main component of the application. It manages the state of the counter and renders the UI elements.
State
- counter: Holds the current value of the counter.
Methods
handleIncrement: Increments the counter value by 1.
handleDecrement: Decrements the counter value by 1.
Rendered UI
Title: Displays the title of the app ("Counter App").
Counter Display: Shows the current value of the counter.
Buttons: Two buttons, one for incrementing (
+
) and the other for decrementing (-
) the counter.
Styles
The stylesheet (styles
) defines the visual appearance of the components.
container: Styles for the main container.
title: Styles for the title text.
counter: Styles for the counter value text.
buttonContainer: Styles for the container of the buttons.
button: Styles for the buttons.
buttonText: Styles for the text inside the buttons.
Usage
Install dependencies:
npm install
oryarn install
.Run the application:
npm start
oryarn start
.Open the app on an emulator or a physical device.
Styling
The application features a clean and visually appealing design with a light background color. The buttons are styled with a bold color to make them stand out. Feel free to customize the styles in the styles
object to match your preferences.
import React, { useState } from 'react'; import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; const App = () => { const [counter, setCounter] = useState(0); const handleIncrement = () => { setCounter(counter + 1); }; const handleDecrement = () => { setCounter(counter - 1); }; return ( <View style={styles.container}> <Text style={styles.title}>Counter App</Text> <Text style={styles.counter}>{counter}</Text> <View style={styles.buttonContainer}> <TouchableOpacity style={styles.button} onPress={handleIncrement}> <Text style={styles.buttonText}>+</Text> </TouchableOpacity> <TouchableOpacity style={styles.button} onPress={handleDecrement}> <Text style={styles.buttonText}>-</Text> </TouchableOpacity> </View> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f5f5f5', }, title: { fontSize: 36, fontWeight: 'bold', marginBottom: 20, }, counter: { fontSize: 48, fontWeight: 'bold', marginBottom: 20, }, buttonContainer: { flexDirection: 'row', }, button: { backgroundColor: '#3498db', paddingVertical: 15, paddingHorizontal: 30, margin: 10, borderRadius: 10, }, buttonText: { color: 'white', fontSize: 24, fontWeight: 'bold', }, }); export default App;