Learn more about React Native components like Buttons , Flatlist and ScrollView
Tags: react, javascript, beginners, tutorial
Published: 2020-12-04 15:56:57

In this post I will discuss some more React Native components like Buttons, Flatlists, ScrollViews etc.,

This is a part of my series on React Native. Make sure you check out the others.

We will be discussing about

1. Buttons.
2. Images.
3. Flatlists.
4. ScrollViews.
5. TouchabeOpacity.

Before using any of them we have to import the components.

```js

import React from 'react'
import {Button, Image, Flatlist, ScrollView, TouchabeOpacity,  TouchableWithoutFeedBack}

```

<strong> 1. Buttons</strong>

Buttons can be used in our react native app by importing them and using them in our code just like in HTML or in React's jsx.

We can use <code>onPress={}</code> prop to give it a functionality when the button is clicked.

In short you can use all the functionality of React's button to a React Native button.

The only difference is, we have to add a <code>title ="Give me a name"</code> prop to add a text to it.

```js

<Button title="Click Me!!"onPress={button}/>

```

<strong> 2. Image </strong>

As same as the button above, Images in react native use the same principles of HTML and React's jsx images with minor differences.

The differences are,

- Source in images is not <code>src</code> but <code>source</code>.

- And to use an uri/url and to export a local image, we have to use something like

```js

   <Image
          source={{
          uri: 'https://coding-magnified.tech',
        }}
      />
 <Image
        style={styles.image}
        source={require('/coding-magnified-logo.png')}
      />


```

You can also use style prop to style both buttons and images in react native.

<strong>3. Flatlists</strong>

Flatlists are the map functions of react native but without the function.

It is used when we want to output a array (list) of data to the frontend.

For using Flatlists we need to use <code>SafeAreaView</code> and not <code>View</code> because we are rendering a list of elements and so to keep them within the area/boundary of the mobile device we have to use this component and wrap it around flatlist. 

Below is the code example,

```js
import {SafeAreaView,FlatList,Text} from 'react-native'

const MY_NAME = [
  {
    id: '1',
    name: 'Gautham',
  },
  {
    id: '2',
    name: 'Vijayan', 
];
return(
<SafeAreaView>
<Flatlist data={MY_NAME}
             keyExtractor={item => item.id}
             renderItem={(item)=>{
             <Text>{item.name}</Text>
}} />
</SafeAreaView>


```

You can see that it is exactly similar to react's(ES6) map function.

Here renderItem is nothing but the function which will render the items out to the frontend and keyExtractor extracts the key out of the array data so we need not use that in renderItem again.

There are a lot more props which can be add to the flatlist, but as this tutorial is for beginners, you can read this wonderful [medium article about flatlist](https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6) which discusses about the other props of flatlist.


<strong>4. ScrollView</strong>

Its the exact copy of flatlist but with a single big difference.

> Flatlist lazy loads the items and ScrollView loads all of the data when the UI first renders.

This may cause performance issues and must be used wisely.

```js

<SafeAreaView>
<ScrollView>
The data which needs to be rendered.
</ScrollView>
</SafeAreaView>

```
<strong> 5. TouchableOpacity </strong>

It can be analogous to the hover motion of mouse in desktop/mac.

When a button/any component in React-Native is touched, we can lower its opacity with this component by wrapping it around the btoon/any other component.

This may seem trivial, but this is an exceptional UX tool for any mobile app as it subtly provides a difference to the user when an touching a button/component.

```js

 <TouchableOpacity>
 <Text> I will have lower opacity when touched</Text>
</TouchableOpacity>

``` 

And that's all the major React-Native tags I use in my projects.

If I want any other Components which will be needed in my projects, I will head on to the [React-Native docs](https://reactnative.dev/docs/) and spend time to learn and apply it to my projects.

In my next post I will discuss about creating navigators for your app which form the backbone of traversing from one page to another in any mobile app.

There a few types of navigators I will discuss.

1. Stack Navigator.
2. Bottom Tab navigator
3. Drawer Navigator

and many more.
### Thank you for reading!!

<strong> <u>Check out my portfolio: </u> [Gautham's portfolio](https://gautham-portfolio.netlify.app/)</strong>

<strong> <u> Check out my blog: </u>[coding-magnified.tech](https://coding-magnified.tech)</strong>

If you want to appreciate my or support me, you can buy me a coffee with the link below. 

If you support me I will make personalized content specifically 
 for you!!

<a href="https://www.buymeacoffee.com/XJvD4NhAK" target="_blank">
<img width="250px" height="100px"src="https://dev-to-uploads.s3.amazonaws.com/i/xwa28cylrit799s70lrs.png" alt="Buy Me a Coffee"</img>
</a>