Debug School

rakesh kumar
rakesh kumar

Posted on

Different kind of widget in react native mobile app

Text
Displays text on the screen.

import React from 'react';
import { Text, StyleSheet, View } from 'react-native';

const TextExample = () => (
  <View style={styles.container}>
    <Text style={styles.text}>Hello, React Native!</Text>
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  text: { fontSize: 20, color: 'blue' }
});

export default TextExample;
Enter fullscreen mode Exit fullscreen mode
  1. View The most basic container for layout purposes.
import React from 'react';
import { View, StyleSheet } from 'react-native';

const ViewExample = () => (
  <View style={styles.container}>
    <View style={styles.box} />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  box: { width: 100, height: 100, backgroundColor: 'red' }
});

export default ViewExample;
Enter fullscreen mode Exit fullscreen mode
  1. TextInput Used for input fields where users can type text.
import React, { useState } from 'react';
import { TextInput, StyleSheet, View } from 'react-native';

const TextInputExample = () => {
  const [text, setText] = useState('');

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        placeholder="Enter text"
        onChangeText={setText}
        value={text}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  input: { height: 40, borderColor: 'gray', borderWidth: 1, width: '80%', paddingLeft: 8 }
});

export default TextInputExample;
Enter fullscreen mode Exit fullscreen mode
  1. Button A button that triggers an action when pressed.
import React from 'react';
import { Button, Alert, View, StyleSheet } from 'react-native';

const ButtonExample = () => (
  <View style={styles.container}>
    <Button title="Press Me" onPress={() => Alert.alert('Button Pressed')} />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

Enter fullscreen mode Exit fullscreen mode

export default ButtonExample;

  1. Image Display images in the app.
import React from 'react';
import { Image, StyleSheet, View } from 'react-native';

const ImageExample = () => (
  <View style={styles.container}>
    <Image
      style={styles.image}
      source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }}
    />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  image: { width: 100, height: 100 }
});

export default ImageExample;
Enter fullscreen mode Exit fullscreen mode
  1. FlatList A performant way to render large lists of data.
import React from 'react';
import { FlatList, Text, StyleSheet, View } from 'react-native';

const FlatListExample = () => {
  const data = ['Apple', 'Banana', 'Orange'];

  return (
    <View style={styles.container}>
      <FlatList
        data={data}
        renderItem={({ item }) => <Text style={styles.item}>{item}</Text>}
        keyExtractor={(item, index) => index.toString()}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  item: { fontSize: 20, marginBottom: 10 }
});

export default FlatListExample;
Enter fullscreen mode Exit fullscreen mode
  1. SectionList A list that supports sections of data.
import React from 'react';
import { SectionList, Text, StyleSheet, View } from 'react-native';

const SectionListExample = () => {
  const sections = [
    { title: 'Fruits', data: ['Apple', 'Banana'] },
    { title: 'Vegetables', data: ['Carrot', 'Broccoli'] },
  ];

  return (
    <View style={styles.container}>
      <SectionList
        sections={sections}
        renderItem={({ item }) => <Text style={styles.item}>{item}</Text>}
        renderSectionHeader={({ section }) => (
          <Text style={styles.header}>{section.title}</Text>
        )}
        keyExtractor={(item, index) => index.toString()}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, paddingTop: 20 },
  item: { fontSize: 20, padding: 10 },
  header: { fontSize: 24, fontWeight: 'bold', backgroundColor: '#f0f0f0' },
});

export default SectionListExample;
Enter fullscreen mode Exit fullscreen mode
  1. TouchableOpacity A wrapper for making elements clickable with a fade effect.
import React from 'react';
import { TouchableOpacity, Text, StyleSheet, View } from 'react-native';

const TouchableOpacityExample = () => (
  <View style={styles.container}>
    <TouchableOpacity style={styles.button} onPress={() => alert('Pressed!')}>
      <Text style={styles.text}>Click Me</Text>
    </TouchableOpacity>
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  button: { backgroundColor: 'blue', padding: 10, borderRadius: 5 },
  text: { color: 'white' }
});

export default TouchableOpacityExample;
Enter fullscreen mode Exit fullscreen mode
  1. TouchableHighlight A clickable component with highlight effect.
import React from 'react';
import { TouchableHighlight, Text, StyleSheet, View } from 'react-native';

const TouchableHighlightExample = () => (
  <View style={styles.container}>
    <TouchableHighlight
      style={styles.button}
      onPress={() => alert('Pressed!')}
      underlayColor="#DDDDDD">
      <Text style={styles.text}>Click Me</Text>
    </TouchableHighlight>
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  button: { backgroundColor: 'blue', padding: 10, borderRadius: 5 },
  text: { color: 'white' }
});

export default TouchableHighlightExample;
Enter fullscreen mode Exit fullscreen mode
  1. Switch A toggle switch component.
import React, { useState } from 'react';
import { Switch, StyleSheet, View, Text } from 'react-native';

const SwitchExample = () => {
  const [isEnabled, setIsEnabled] = useState(false);

  return (
    <View style={styles.container}>
      <Text>{isEnabled ? 'Switch is ON' : 'Switch is OFF'}</Text>
      <Switch value={isEnabled} onValueChange={setIsEnabled} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default SwitchExample;
Enter fullscreen mode Exit fullscreen mode
  1. ActivityIndicator A loading spinner that indicates activity.
import React from 'react';
import { ActivityIndicator, StyleSheet, View } from 'react-native';

const ActivityIndicatorExample = () => (
  <View style={styles.container}>
    <ActivityIndicator size="large" color="#0000ff" />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default ActivityIndicatorExample;
Enter fullscreen mode Exit fullscreen mode
  1. Picker Allows the selection of an item from a dropdown.
import React, { useState } from 'react';
import { Picker, StyleSheet, View } from 'react-native';

const PickerExample = () => {
  const [selectedValue, setSelectedValue] = useState('apple');

  return (
    <View style={styles.container}>
      <Picker
        selectedValue={selectedValue}
        onValueChange={(itemValue) => setSelectedValue(itemValue)}>
        <Picker.Item label="Apple" value="apple" />
        <Picker.Item label="Banana" value="banana" />
        <Picker.Item label="Orange" value="orange" />
      </Picker>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default PickerExample;
Enter fullscreen mode Exit fullscreen mode
  1. Modal A popup that can display content.
import React, { useState } from 'react';
import { Modal, Button, StyleSheet, View, Text } from 'react-native';

const ModalExample = () => {
  const [modalVisible, setModalVisible] = useState(false);

  return (
    <View style={styles.container}>
      <Button title="Show Modal" onPress={() => setModalVisible(true)} />
      <Modal
        animationType="slide"
        transparent={true}
        visible={modalVisible}
        onRequestClose={() => setModalVisible(false)}>
        <View style={styles.modalView}>
          <Text style={styles.text}>This is a Modal!</Text>
          <Button title="Hide Modal" onPress={() => setModalVisible(false)} />
        </View>
      </Modal>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  modalView: {
    backgroundColor: 'white',
    padding: 20,
    marginTop: 100,
    borderRadius: 10
  },
  text: { marginBottom: 15 }
});

export default ModalExample;
Enter fullscreen mode Exit fullscreen mode
  1. Slider A slider for selecting a value from a range.
import React, { useState } from 'react';
import { Slider, StyleSheet, View, Text } from 'react-native';

const SliderExample = () => {
  const [sliderValue, setSliderValue] = useState(0);

  return (
    <View style={styles.container}>
      <Slider
        style={styles.slider}
        minimumValue={0}
        maximumValue={100}
        step={1}
        value={sliderValue}
        onValueChange={setSliderValue}
      />
      <Text>Value: {sliderValue}</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  slider: { width: 200, height: 40 }
});

export default SliderExample;
Enter fullscreen mode Exit fullscreen mode
  1. DatePickerIOS Displays an iOS date picker.
import React, { useState } from 'react';
import { DatePickerIOS, StyleSheet, View, Text } from 'react-native';

const DatePickerExample = () => {
  const [date, setDate] = useState(new Date());

  return (
    <View style={styles.container}>
      <DatePickerIOS date={date} onDateChange={setDate} />
      <Text>Selected Date: {date.toLocaleDateString()}</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default DatePickerExample;
Enter fullscreen mode Exit fullscreen mode
  1. Linking Open external URLs from the app.
import React from 'react';
import { Linking, Button, StyleSheet, View } from 'react-native';

const LinkingExample = () => (
  <View style={styles.container}>
    <Button title="Open Google" onPress={() => Linking.openURL('https://www.google.com')} />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default LinkingExample;
Enter fullscreen mode Exit fullscreen mode
  1. StatusBar Modify the status bar appearance.
import React from 'react';
import { StatusBar, StyleSheet, View } from 'react-native';

const StatusBarExample = () => (
  <View style={styles.container}>
    <StatusBar barStyle="dark-content" backgroundColor="#6a51ae" />
  </View>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default StatusBarExample;
Enter fullscreen mode Exit fullscreen mode
  1. TouchableWithoutFeedback Handles touch events without affecting the touchable area.
import React from 'react';
import { TouchableWithoutFeedback, Keyboard, StyleSheet, View } from 'react-native';

const TouchableWithoutFeedbackExample = () => (
  <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
    <View style={styles.container}>
      <Text>Tap anywhere to dismiss the keyboard</Text>
    </View>
  </TouchableWithoutFeedback>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default TouchableWithoutFeedbackExample;
Enter fullscreen mode Exit fullscreen mode
  1. SafeAreaView Ensures content is not overlapped by notches or status bars.
import React from 'react';
import { SafeAreaView, Text, StyleSheet } from 'react-native';

const SafeAreaViewExample = () => (
  <SafeAreaView style={styles.container}>
    <Text>Content safe from notches and status bars!</Text>
  </SafeAreaView>
);

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
});

export default SafeAreaViewExample;
Enter fullscreen mode Exit fullscreen mode
  1. KeyboardAvoidingView Avoids keyboard overlap with input fields.
import React, { useState } from 'react';
import { TextInput, Button, StyleSheet, KeyboardAvoidingView, View } from 'react-native';

const KeyboardAvoidingViewExample = () => {
  const [text, setText] = useState('');

  return (
    <KeyboardAvoidingView style={styles.container} behavior="padding">
      <TextInput
        style={styles.input}
        placeholder="Type here"
        value={text}
        onChangeText={setText}
      />
      <Button title="Submit" onPress={() => console.log(text)} />
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  input: { height: 40, borderColor: 'gray', borderWidth: 1, width: '80%', paddingLeft: 8 }
});

export default KeyboardAvoidingViewExample;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)