Categories
JavaScript Answers React Native Answers

How to check when the focus is lost in a React Native TextInput?

Sometimes, we want to check when the focus is lost in a React Native TextInput.

In this article, we’ll look at how to check when the focus is lost in a React Native TextInput.

How to check when the focus is lost in a React Native TextInput?

To check when the focus is lost in a React Native TextInput, we can set the onBlur prop to a function that runs when focus is lost.

For instance, we write:

import * as React from 'react';
import { TextInput, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View>
      <TextInput onBlur={() => console.log('focus lost')} placeholder="Name" />
    </View>
  );
}

to set the onBlur prop to a function that logs 'focus lost' when we move focus away from the text input.

Conclusion

To check when the focus is lost in a React Native TextInput, we can set the onBlur prop to a function that runs when focus is lost.

Categories
JavaScript Answers React Native

How to add a floating action button on React Native

Sometimes, we want to add a floating action button on React Native.

In this article, we’ll look at how to add a floating action button on React Native.

How to add a floating action button on React Native

To add a floating action button on React Native, we can use the TouchableOpacity and Icon components.

For instance, we write:

import * as React from 'react';
import { TouchableOpacity, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}>
      <TouchableOpacity
        style={{
          borderWidth: 1,
          borderColor: 'rgba(0,0,0,0.2)',
          alignItems: 'center',
          justifyContent: 'center',
          width: 70,
          position: 'absolute',
          bottom: 10,
          right: 10,
          height: 70,
          backgroundColor: '#fff',
          borderRadius: 100,
        }}>
        <Icon name="plus" size={30} color="#01a699" />
      </TouchableOpacity>
    </View>
  );
}

to position that TouchableOpacity component with position, bottom, and right properties.

We set borderRadius to 100 to make it round.

And we set justifyContent and alignItems to 'center' to center the contents.

Next, we add an Icon in it to show an icon inside the button.

Conclusion

To add a floating action button on React Native, we can use the TouchableOpacity and Icon components.

Categories
JavaScript Answers React Native Answers

How to change the height of a FlatList in React Native?

Sometimes, we want to change the height of a FlatList in React Native.

In this article, we’ll look at how to change the height of a FlatList in React Native.

How to change the height of a FlatList in React Native?

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const flatListItems = Array(200)
  .fill()
  .map((_, i) => ({ title: i, id: i }));

const Item = ({ title }) => (
  <View
    style={{
      backgroundColor: '#f9c2ff',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
    }}>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View style={{ height: 300 }}>
      <FlatList
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to set the height of the View to 300px to make the height of the FlatList 300px.

Conclusion

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.

Categories
JavaScript Answers React Native Answers

How to change background color of React Native button?

Sometimes, we want to change background color of React Native button.

In this article, we’ll look at how to change background color of React Native button.

How to change background color of React Native button?

To change background color of React Native button, we can set the color prop for Android and we set the backgroundColor for iOS.

For instance, we write:

import * as React from 'react';
import { Button, View, TouchableOpacity, Text } from 'react-native';
import Constants from 'expo-constants';

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <Button title="Login Android" color="#1E6738" />
      <TouchableOpacity style={{ backgroundColor: '#1E6738' }}>
        <Text style={{ color: '#fff', textAlign: 'center' }}>Login iOS</Text>
      </TouchableOpacity>
    </View>
  );
}

to add <Button title="Login Android" color="#1E6738" /> add a button with the background color set to #1E6738 for Android.

Then for iOS, we write set the backgroundColor on the TouchableOpacity component to add the background color.

And we set the text color in the Text component.

Conclusion

To change background color of React Native button, we can set the color prop for Android and we set the backgroundColor for iOS.

Categories
JavaScript Answers React Native Answers

How to centralize text in React Native TextInput?

Sometimes, we want to centralize text in React Native TextInput.

In this article, we’ll look at how to centralize text in React Native TextInput.

How to centralize text in React Native TextInput?

To centralize text in React Native TextInput, we can set the textAlign prop to 'center'.

For instance, we write:

import * as React from 'react';
import { TextInput, View } from 'react-native';
import Constants from 'expo-constants';

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <TextInput textAlign="center" placeholder="Your Account" />
    </View>
  );
}

to set the textAlign prop to 'center' to make the placeholder and input value centered on the screen.

Conclusion

To centralize text in React Native TextInput, we can set the textAlign prop to 'center'.