React Native Components: Usage and Features

 

Tulpar Yazılım - React Native Components

 

React Native is a JavaScript-based mobile app development framework. Its component-based nature provides the power and flexibility of React Native. In this blog post, we will explore the usage and features of the core components available in React Native. Using React Native components properly gives you control over the look and behavior of your app.

  1. View Component: The View component is the ancestor of many core components and creates a container on your application's screen. In this section, you can explain how to use the View component and how to style it. You can also examine the View component's features such as layout management and flexbox usage.


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

    const ViewBoxesWithColorAndText = () => {
      return (
        <View
          style={{
            flexDirection: 'row',
            height: 100,
            padding: 20,
          }}>
          <View style={{backgroundColor: 'blue', flex: 0.3}} />
          <View style={{backgroundColor: 'red', flex: 0.5}} />
          <Text>Hello World!</Text>
        </View>
      );
    };

    export default ViewBoxesWithColorAndText;

  2. Text Component: The text component is used to display text in your application. In this section, you can explain how to use the Text component, how to set the text style, and how to use the text formatting options. You can also handle built-in text components (eg Heading, Paragraph) and text localization. 


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

    const BoldAndBeautiful = () => {
      return (
        <Text style={styles.baseText}>
          I am bold
          <Text style={styles.innerText}> and red</Text>
        </Text>
      );
    };

    const styles = StyleSheet.create({
      baseText: {
        fontWeight: 'bold',
      },
      innerText: {
        color: 'red',
      },
    });

    export default BoldAndBeautiful;

  3. Image Component: The Image component is used to display images in your application. In this section, you can explain how to use the Image component, load an image locally or remotely, and customize the display style. You can also touch on related topics such as image optimization and access to local resources.


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

    const styles = StyleSheet.create({
      container: {
        paddingTop: 50,
      },
      tinyLogo: {
        width: 50,
        height: 50,
      },
      logo: {
        width: 66,
        height: 58,
      },
    });

    const DisplayAnImage = () => {
      return (
        <View style={styles.container}>
          <Image
            style={styles.tinyLogo}
            source={require('@expo/snack-static/react-native-logo.png')}
          />
          <Image
            style={styles.tinyLogo}
            source={{
              uri: 'https://reactnative.dev/img/tiny_logo.png',
            }}
          />
          <Image
            style={styles.logo}
            source={{
              uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg==',
            }}
          />
        </View>
      );
    };

    export default DisplayAnImage;

  4. Button Component: The button component creates a button that the user can interact with. In this section, you can explain how to use the Button component, how to create simple or custom button styles, and how to react to button events. You can also handle button features like touch feedback and accessibility.


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

    const Separator = () => <View style={styles.separator} />;

    const App = () => (
      <SafeAreaView style={styles.container}>
        <View>
          <Text style={styles.title}>
            The title and onPress handler are required. It is recommended to set
            accessibilityLabel to help make your app usable by everyone.
          </Text>
          <Button
            title="Press me"
            onPress={() => Alert.alert('Simple Button pressed')}
          />
        </View>
        <Separator />
        <View>
          <Text style={styles.title}>
            Adjust the color in a way that looks standard on each platform. On iOS,
            the color prop controls the color of the text. On Android, the color
            adjusts the background color of the button.
          </Text>
          <Button
            title="Press me"
            color="#f194ff"
            onPress={() => Alert.alert('Button with adjusted color pressed')}
          />
        </View>
        <Separator />
        <View>
          <Text style={styles.title}>
            All interaction for the component are disabled.
          </Text>
          <Button
            title="Press me"
            disabled
            onPress={() => Alert.alert('Cannot press this one')}
          />
        </View>
        <Separator />
        <View>
          <Text style={styles.title}>
            This layout strategy lets the title define the width of the button.
          </Text>
          <View style={styles.fixToText}>
            <Button
              title="Left button"
              onPress={() => Alert.alert('Left button pressed')}
            />
            <Button
              title="Right button"
              onPress={() => Alert.alert('Right button pressed')}
            />
          </View>
        </View>
      </SafeAreaView>
    );

    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        marginHorizontal: 16,
      },
      title: {
        textAlign: 'center',
        marginVertical: 8,
      },
      fixToText: {
        flexDirection: 'row',
        justifyContent: 'space-between',
      },
      separator: {
        marginVertical: 8,
        borderBottomColor: '#737373',
        borderBottomWidth: StyleSheet.hairlineWidth,
      },
    });

    export default App;

  5. TextInput Component: The TextInput component creates a field where the user can input text. In this section, you can explain how to use the TextInput component, apply text input restrictions, and react to text input events. You can also cover related topics such as text validation and keyboard types.


    import React from 'react';
    import {SafeAreaView, StyleSheet, TextInput} from 'react-native';

    const TextInputExample = () => {
      const [text, onChangeText] = React.useState('Useless Text');
      const [number, onChangeNumber] = React.useState('');

      return (
        <SafeAreaView>
          <TextInput
            style={styles.input}
            onChangeText={onChangeText}
            value={text}
          />
          <TextInput
            style={styles.input}
            onChangeText={onChangeNumber}
            value={number}
            placeholder="useless placeholder"
            keyboardType="numeric"
          />
        </SafeAreaView>
      );
    };

    const styles = StyleSheet.create({
      input: {
        height: 40,
        margin: 12,
        borderWidth: 1,
        padding: 10,
      },
    });

    export default TextInputExample;

 

In this blog post, we explored the usage and features of key components available in React Native. Components such as View, Text, Image, Button, and TextInput help you control the appearance and user interaction of your application. Used correctly, these components help improve your app's user experience. I suggest you consult the official documentation to explore more React Native components and use their features.

Would you like to have an interview?

We help you with your business's most critical issues and opportunities. How about getting permanent change and results together?