Categories
JavaScript Answers React Native Answers

How to add a horizontal FlatList in React Native?

Sometimes, we want to add a horizontal FlatList in React Native.

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

How to add a horizontal FlatList in React Native?’

To add a horizontal FlatList in React Native, we just set the horizontal prop of the FlatList to true.

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 keyExtractor = (item) => item.id;

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

to add horizontal to FlatList.

As a result, we can scroll through the FlatList items horizontally.

Conclusion

To add a horizontal FlatList in React Native, we just set the horizontal prop of the FlatList to true.

Categories
JavaScript Answers React Native Answers

How to set fixed width to individual characters with React Native?

Sometimes, we want to set fixed width to individual characters with React Native.

In this article, we’ll look at how to set fixed width to individual characters with React Native.

How to set fixed width to individual characters with React Native?

To set fixed width to individual characters with React Native, we set the font to a monospaced font.

For instance, we write:

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

export default function App() {
  const fontFamily = Platform.OS === 'ios' ? 'Courier' : 'monospace';

  return (
    <View style={{ padding: 30 }}>
      <Text style={{ fontFamily }}>hello world</Text>
    </View>
  );
}

to set the fontFamily to a monospaced font according to the platform.

If it’s iOS, we set it to 'Courier'.

Otherwise, we use 'monospace'.

We get the OS the device is running on with Platform.OS.

Conclusion

To set fixed width to individual characters with React Native, we set the font to a monospaced font.

Categories
JavaScript Answers React Native Answers

How to get the value in TextInput when onBlur is called with React Native?

Sometimes, we want to get the value in TextInput when onBlur is called with React Native.

In this article, we’ll look at how to get the value in TextInput when onBlur is called with React Native.

How to get the value in TextInput when onBlur is called with React Native?

To get the value in TextInput when onBlur is called with React Native, we can get the value from the onEndEditing callback.

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';

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <TextInput
        onEndEditing={(e) => console.log(e.nativeEvent.text)}
        placeholder="Name"
      />
    </View>
  );
}

to set onEndEditing to (e) => console.log(e.nativeEvent.text) after we press enter.

Conclusion

To get the value in TextInput when onBlur is called with React Native, we can get the value from the onEndEditing callback.

Categories
JavaScript Answers React Native Answers

How to add space between two elements on the same line in React Native?

Sometimes, we want to add space between two elements on the same line in React Native.

In this article, we’ll look at how to add space between two elements on the same line in React Native.

How to add space between two elements on the same line in React Native?

To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
      <Text>foo</Text>
      <Text>bar</Text>
    </View>
  );
}

to set the flexDirection to 'row' and the justifyContent style to 'space-between' to spread the Text components on the screen.

As a result, we should see ‘foo’ displayed on the left side and ‘bar’ on the right.

Conclusion

To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View.

Categories
JavaScript Answers React Native Answers

How to disable iOS keyboard suggestions in React Native?

Sometimes, we want to disable iOS keyboard suggestions in React Native.

In this article, we’ll look at how to disable iOS keyboard suggestions in React Native.

How to disable iOS keyboard suggestions in React Native?

To disable iOS keyboard suggestions in React Native, we can set the autoCorrect prop to false.

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';

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <TextInput autoCorrect={false} />
    </View>
  );
}

to set autoCorrect to false.

Now the keyboard suggestions should be hidden in iOS.

Conclusion

To disable iOS keyboard suggestions in React Native, we can set the autoCorrect prop to false.