Categories
JavaScript Answers React Native Answers

How to have content break to next line with flex when content reaches edge with React Native?

Sometimes, we want to have content break to next line with flex when content reaches edge with React Native.

In this article, we’ll look at how to have content break to next line with flex when content reaches edge with React Native.

How to have content break to next line with flex when content reaches edge with React Native?

To have content break to next line with flex when content reaches edge with React Native, we can set flexWrap to 'wrap'.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import NumberFormat from 'react-number-format';

export default function App() {
  return (
    <View style={{ flexWrap: 'wrap', flexDirection: 'row' }}>
      {Array(20)
        .fill()
        .map((_, i) => {
          return (
            <Text key={i} style={{ width: 50 }}>
              {i}
            </Text>
          );
        })}
    </View>
  );
}

to set flexWrap to 'wrap' in the View so that the Text components inside will wrap to the next row when they reach the screen’s right edge.

Conclusion

To have content break to next line with flex when content reaches edge with React Native, we can set flexWrap to 'wrap'.

Categories
JavaScript Answers React Native Answers

How to format a number to currency when using React Native?

Sometimes, we want to format a number to currency when using React Native.

In this article, we’ll look at how to format a number to currency when using React Native.

How to format a number to currency when using React Native?

To format a number to currency when using React Native, we can use the react-number-format package.

To install it, we run npm i react-number-format.

Then we use it by writing:

import * as React from 'react';
import { View, Text } from 'react-native';
import NumberFormat from 'react-number-format';

export default function App() {
  return (
    <View>
      <NumberFormat
        value={7585945}
        displayType="text"
        thousandSeparator
        prefix="$"
        renderText={(value) => <Text>{value}</Text>}
      />
    </View>
  );
}

to add the NumberFormat component.

value is set to the number to format.

displayType sets how the formatted number should be rendered.

thousandSeparator adds thousand separators to the return number.

prefix sets the prefix to add to the number.

renderText is set a function that renders the value in the Text component.

Conclusion

To format a number to currency when using React Native, we can use the react-number-format package.

To install it, we run npm i react-number-format.

Categories
JavaScript Answers React Native Answers

How to get the index of the currently visible item in a React Native flat list?

Sometimes, we want to get the index of the currently visible item in a React Native flat list.

In this article, we’ll look at how to get the index of the currently visible item in a React Native flat list.

How to get the index of the currently visible item in a React Native flat list?

To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items.

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} />;
  const onViewableItemsChanged = ({ viewableItems, changed }) => {
    console.log(viewableItems);
    console.log(changed);
  };

  return (
    <View>
      <FlatList
        onViewableItemsChanged={onViewableItemsChanged}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to define the onViewableItemsChanged function that gets the viewableItems property.

viewableItems has the items that are current visible on the screen.

changed has the items that have just entered or left after the last scroll.

They’re both arrays.

Conclusion

To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items.

Categories
JavaScript Answers React Native Answers

How to capitalize first letter of text in React Native?

Sometimes, we want to capitalize first letter of text in React Native.

In this article, we’ll look at how to capitalize first letter of text in React Native.

How to capitalize first letter of text in React Native?

To capitalize first letter of text in React Native, we can create our own function.

For instance, we write:

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

const capitalize = (str) => {
  return str[0].toUpperCase() + str.slice(1);
};

export default function App() {
  return (
    <View>
      <Text>{capitalize('hello')}</Text>
    </View>
  );
}

to define the capitalize function that returns a string with the first letter of str capitalized.

We call toUpperCase to capitalize the first letter and slice with 1 to return the characters in str starting from index 1 to the end of the string.

As a result, we see ‘Hello’ displayed.

Conclusion

To capitalize first letter of text in React Native, we can create our own function.

Categories
JavaScript Answers React Native Answers

How to disable a View component in React Native?

Sometimes, we want to disable a View component in React Native.

In this article, we’ll look at how to disable a View component in React Native.

How to disable a View component in React Native?

To disable a View component in React Native, we can set the pointerEvents prop to 'none'.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <View pointerEvents="none">...</View>
    </View>
  );
}

to set the inner view’s pointerEvents prop to 'none' so that users can’t interact with the content inside.

Conclusion

To disable a View component in React Native, we can set the pointerEvents prop to 'none'.