Categories
JavaScript Answers React Native Answers

How to change font family for TextInput placeholder in React Native?

Sometimes, we want to change font family for TextInput placeholder in React Native.

In this article, we’ll look at how to change font family for TextInput placeholder in React Native.

How to change font family for TextInput placeholder in React Native?

To change font family for TextInput placeholder in React Native, we can set the fontFamily style of the TextInput.

For instance, we write:

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

export default function App() {
  const [value, setValue] = React.useState();

  return (
    <View>
      <TextInput
        style={{ fontFamily: 'courier' }}
        value={value}
        onChangeText={setValue}
      />
    </View>
  );
}

to set the fontFamily property to 'courier' to render the text inputted in the TextInput with Courier.

Conclusion

To change font family for TextInput placeholder in React Native, we can set the fontFamily style of the TextInput.

Categories
JavaScript Answers React Native Answers

How to detect swipe left in React Native?

Sometimes, we want to detect swipe left in React Native.

In this article, we’ll look at how to detect swipe left in React Native.

How to detect swipe left in React Native?

To detect swipe left in React Native, we can use the react-native-swipe-gestures package.

To install it, we run npm i react-native-swipe-gestures.

Then we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import GestureRecognizer, {
  swipeDirections,
} from 'react-native-swipe-gestures';

export default function App() {
  const onSwipe = (gestureName, gestureState) => {
    const { SWIPE_UP, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT } = swipeDirections;
    console.log(gestureName);
  };

  return (
    <GestureRecognizer onSwipe={onSwipe}>
      <Text style={{ padding: 50 }}>hello</Text>
    </GestureRecognizer>
  );
}

to add the GestureRecognizer component which wraps around any component we want to detect swipes for.

Then we set the onSwipe prop to the onSwipe function.

And we can get the swipe direction from the gestureName.

gestureName is one of 'SWIPE_UP', 'SWIPE_DOWN', 'SWIPE_LEFT', 'SWIPE_RIGHT'.

gestureName is 'SWIPE_LEFT' when we swipe left.

Conclusion

To detect swipe left in React Native, we can use the react-native-swipe-gestures package.

To install it, we run npm i react-native-swipe-gestures.

Categories
JavaScript Answers React Native Answers

How to check if an array is empty in React Native?

Sometimes, we want to check if an array is empty in React Native.

In this article, we’ll look at how to check if an array is empty in React Native.

How to check if an array is empty in React Native?

To check if an array is empty in React Native, we can use the Array.isArray method and the array length property.

For instance, we write:

import * as React from 'react';
import { View } from 'react-native';

export default function App() {
  const [arr] = React.useState([]);

  if (Array.isArray(arr) && arr.length === 0) {
    console.log('is empty');
  }

  return <View></View>;
}

to check if arr is an empty array with Array.isArray(arr) && arr.length === 0.

Array.isArray checks if its argument is an array.

arr.length === 0 checks if arr is empty.

Conclusion

To check if an array is empty in React Native, we can use the Array.isArray method and the array length property.

Categories
JavaScript Answers React Native Answers

How to style an Alert element in React Native?

Sometimes, we want to style an Alert element in React Native.

In this article, we’ll look at how to style an Alert element in React Native.

How to style an Alert element in React Native?

To style an Alert element in React Native, we can use the react-native-modalbox package.

We run npm i react-native-modalbox to install it.

Then we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import Modal from 'react-native-modalbox';

export default function App() {
  const ref = React.useRef();

  React.useEffect(() => {
    ref.current.open();
  }, []);

  return (
    <View>
      <Modal style={{ background: 'red' }} ref={ref}>
        <Text style={{ color: 'green' }}>Basic modal</Text>
      </Modal>
    </View>
  );
}

to add the Modal component from the package.

We assign a ref to it and then we call open on it to open the modal.

Conclusion

To style an Alert element in React Native, we can use the react-native-modalbox package.

Categories
JavaScript Answers React Native Answers

How to add a click listener in FlatList with React Native?

Sometimes, we want to add a click listener in FlatList with React Native.

In this article, we’ll look at how to add a click listener in FlatList with React Native.

How to add a click listener in FlatList with React Native?

To add a click listener in FlatList with React Native, we can set the onPress prop to a function.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View, Dimensions } 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 = (item) => {
  const { title } = item;
  return (
    <View
      style={{
        backgroundColor: '#f9c2ff',
        padding: 20,
        marginVertical: 8,
        marginHorizontal: 16,
      }}>
      <Text onPress={() => console.log(item)}>{title}</Text>
    </View>
  );
};

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

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

to set onPress to a function that logs the item in the Item component.

As a result, when we press on the text, we see the item object logged.

Conclusion

To add a click listener in FlatList with React Native, we can set the onPress prop to a function.