Categories
JavaScript Answers React Native Answers

How to add a ListView grid in React Native?

Sometimes, we want to add a ListView grid in React Native

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

How to add a ListView grid in React Native?

To add a ListView grid in React Native, we should use a FlatList instead with some flexbox styles.

For instance, we write:

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

const numColumns = 3;
const size = Dimensions.get('window').width / numColumns;

const styles = StyleSheet.create({
  itemContainer: {
    width: size,
    height: size,
  },
  item: {
    flex: 1,
    margin: 3,
    backgroundColor: 'lightblue',
  },
});

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

export default function App() {
  return (
    <FlatList
      data={data}
      renderItem={({ item }) => (
        <View style={styles.itemContainer}>
          <Text style={styles.item}>{item.value}</Text>
        </View>
      )}
      keyExtractor={(item) => item.id}
      numColumns={numColumns}
    />
  );
}

to add the itemContainer style property that sets the width and height of the container.

Then we render a FlatList with the data array set as the value of the data prop.

Then we set the renderItem prop to a function to render items in the FlatList.

We set numColumns to set the number of columns.

keyExtractor is set to a function that returns the unique key for each item.

Conclusion

To add a ListView grid in React Native, we should use a FlatList instead with some flexbox styles.

Categories
JavaScript Answers React Native Answers

How to scroll to top of the ScrollView with React Native?

Sometimes, we want to scroll to top of the ScrollView with React Native.

In this article, we’ll look at how to scroll to top of the ScrollView with React Native.

How to scroll to top of the ScrollView with React Native?

To scroll to top of the ScrollView with React Native, we assign a ref to the ScrollView and call scrollTo on the ref’s value.

For instance, we write:

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

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

  return (
    <View
      style={{
        flexGrow: 1,
      }}>
      <Button
        title="scroll to top"
        onPress={() => {
          ref.current.scrollTo(0);
        }}
      />
      <ScrollView ref={ref} style={{ height: 200 }}>
        {Array(200)
          .fill()
          .map((_, i) => {
            return <Text>{i}</Text>;
          })}
      </ScrollView>
    </View>
  );
}

to create a ref with useRef and set that as the value of the ref prop of the ScrollView.

Then we add a Button that calls ref.current.scrollTo with 0 to scroll the ScrollView to the top when it’s pressed.

Conclusion

To scroll to top of the ScrollView with React Native, we assign a ref to the ScrollView and call scrollTo on the ref’s value.

Categories
JavaScript Answers React Native Answers

How to add the blur effect with React Native?

Sometimes, we want to add the blur effect with React Native.

In this article, we’ll look at how to add the blur effect with React Native.

How to add the blur effect with React Native?

To add the blur effect with React Native, we can set the blurRadius of the Image.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <Image
        resizeMode="cover"
        source="https://picsum.photos/200/300"
        blurRadius={6}
        style={{ width: 200, height: 300 }}
      />
    </View>
  );
}

to add the Image component to add an image.

Then we set the blurRadius prop to a positive number to add the blur effect.

Conclusion

To add the blur effect with React Native, we can set the blurRadius of the Image.

Categories
JavaScript Answers React Native Answers

How to remove extra space below the TextInput in the KeyboardAvoidingView with React Native?

Sometimes, we want to remove extra space below the TextInput in the KeyboardAvoidingView with React Native.

In this article, we’ll look at how to remove extra space below the TextInput in the KeyboardAvoidingView with React Native.

How to remove extra space below the TextInput in the KeyboardAvoidingView with React Native?

To remove extra space below the TextInput in the KeyboardAvoidingView with React Native, we shoule make KeyboardAvoidingView a child of ScrollView.

For instance, we write:

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

export default function App() {
  return (
    <ScrollView>
      <KeyboardAvoidingView style={{ flex: 1 }} behavior="padding">
        <ScrollView>
          <TextInput />
          <TextInput />
          <TextInput />
          <TextInput />
          <TextInput />
          <TextInput />
        </ScrollView>
      </KeyboardAvoidingView>
    </ScrollView>
  );
}

to wrap the ScrollView around the KeyboardAvoidingView to remove extra space below the TextInputs.

Conclusion

To remove extra space below the TextInput in the KeyboardAvoidingView with React Native, we shoule make KeyboardAvoidingView a child of ScrollView.

Categories
JavaScript Answers React Native Answers

How to use moment.js in React Native?

Sometimes, we want to use moment.js in React Native.

In this article, we’ll look at how to use moment.js in React Native.

How to use moment.js in React Native?

To use moment.js in React Native, we can install it with NPM.

To install it, we run

npm install moment --save

Then we use it by writing

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

export default function App() {
  return (
    <View>
      <Text>{moment().format('YYYY-MM-DD')}</Text>
    </View>
  );
}

We import it with import moment from 'moment'.

Then we use moment().format('YYYY-MM-DD') to show the current date in YYYY-MM-DD format.

Conclusion

To use moment.js in React Native, we can install it with NPM.