Categories
JavaScript Answers React Native Answers

How to enable or disable scrolling on FlatList with React Native?

Sometimes, we want to enable or disable scrolling on FlatList with React Native.

In this article, we’ll look at how to enable or disable scrolling on FlatList with React Native.

How to enable or disable scrolling on FlatList with React Native?

To enable or disable scrolling on FlatList with React Native, we can set the scrollEnabled prop.

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 = ({ 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>
      <FlatList
        scrollEnabled={false}
        style={{ height: 300 }}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      />
    </View>
  );
}

to set the scrollEnabled prop to false to disable scrolling on the FlatList.

Conclusion

To enable or disable scrolling on FlatList with React Native, we can set the scrollEnabled prop.

Categories
JavaScript Answers React Native Answers

How to fix textDecoration properties not working on Android with React Native?

Sometimes, we want to fix textDecoration properties not working on Android with React Native.

In this article, we’ll look at how to fix textDecoration properties not working on Android with React Native.

How to fix textDecoration properties not working on Android with React Native?

To fix textDecoration properties not working on Android with React Native, we can set the textDecorationLine, textDecorationStyle and textDecorationColor styles.

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>
      <Text
        style={{
          textDecorationLine: 'underline',
          textDecorationStyle: 'solid',
          textDecorationColor: '#000',
        }}>
        hello
      </Text>
    </View>
  );
}

to set textDecorationLine to 'underline' to add text underline.

textDecorationStyle set to 'solid' makes the underline solid.

textDecorationColor set the underline color.

Conclusion

To fix textDecoration properties not working on Android with React Native, we can set the textDecorationLine, textDecorationStyle and textDecorationColor styles.

Categories
JavaScript Answers React Native Answers

How to add a 2 column layout with flexbox on React Native?

Somwtimes, we want to add a 2 column layout with flexbox on React Native.

In this article, we’ll look at how to add a 2 column layout with flexbox on React Native.

How to add a 2 column layout with flexbox on React Native?

To add a 2 column layout with flexbox on React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.

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', flexWrap: 'wrap' }}>
      {Array(100)
        .fill()
        .map((_, i) => {
          return (
            <View style={{ width: '50%' }}>
              <Text>{i}</Text>
            </View>
          );
        })}
    </View>
  );
}

to set flexDirection and flexWrap so we get a horizontal flex layout and we wrap overflowing components to the next row.

Then we set each View‘s width to 50% so that they take half the width of the screen.

Conclusion

To add a 2 column layout with flexbox on React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.

Categories
JavaScript Answers React Native Answers

How to set width style to a percentage number with React Native?

Sometimes, we want to set width style to a percentage number with React Native.

In this article, we’ll look at how to set width style to a percentage number with React Native.

How to set width style to a percentage number with React Native?

To set width style to a percentage number with React Native, we can use flexbox.

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' }}>
      <Text style={{ flexGrow: 1 }}>foo</Text>
      <Text style={{ width: '20%' }}>bar</Text>
    </View>
  );
}

to set flexDirection to 'row' to add a horizontal flexbox layout.

We set the 2nd Text component to fill 20% of the screen.

Then we set flexGrow to 1 on the first Text component to expand to fill the remaining width of the screen.

Conclusion

To set width style to a percentage number with React Native, we can use flexbox.

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 style of the FlatList.

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 = ({ 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>
      <FlatList
        style={{ height: 300 }}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      />
    </View>
  );
}

to set the height of the FlatList to 300px.

Conclusion

To change the height of a FlatList in React Native, we can set the height style of the FlatList.