Categories
JavaScript Answers React Native Answers

How to determine if a device is iPhone or iPad with React Native?

Sometimes, we want to determine if a device is iPhone or iPad with React Native

In this article, we’ll look at how to determine if a device is iPhone or iPad with React Native.

How to determine if a device is iPhone or iPad with React Native?

To determine if a device is iPhone or iPad with React Native, we can use the Platform.isPad property.

If it’s true, then it’s an iPad. If it’s false, it’s an iPhone.

Otherwise, it’s undefined.

For instance, we write:

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

export default function App() {
  console.log(Platform.isPad);

  return <View style={{ height: 300 }}></View>;
}

to log the value of Platform.isPad.

If it’s true, then it’s an iPad.

Conclusion

To determine if a device is iPhone or iPad with React Native, we can use the Platform.isPad property.

If it’s true, then it’s an iPad. If it’s false, it’s an iPhone.

Otherwise, it’s undefined.

Categories
JavaScript Answers React Native Answers

How to invert a FlatList with React Native?

Sometimes, we want to invert a FlatList with React Native.

In this article, we’ll look at how to invert a FlatList with React Native.

How to invert a FlatList with React Native?

To invert a FlatList with React Native, we can add the inverted prop.

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} />;

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

to add the inverted prop to the FlatList.

Now the items are displayed in reversed order and we scroll up to scroll through the list instead of down.

Conclusion

To invert a FlatList with React Native, we can add the inverted prop.

Categories
JavaScript Answers React Native Answers

How to store tokens in React Native?

Sometimes, we want to store tokens in React Native.

In this article, we’ll look at how to store tokens in React Native.

How to store tokens in React Native?

To store tokens in React Native, we can use the expo-secure-store package.

To install it, we run expo install expo-secure-store.

Then we write:

import * as React from 'react';
import { Text, View, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import * as SecureStore from 'expo-secure-store';

export default function App() {
  const getToken = async () => {
    await SecureStore.setItemAsync('secureToken', 'secret');
    const token = await SecureStore.getItemAsync('secureToken');
    console.log(token);
  };

  React.useEffect(() => {
    getToken();
  }, []);

  return <View></View>;
}

to call SecureStore.setItemAsync with the key and value to store the entry.

And then we call SecureStore.getItemAsync with the key to return a promise with the entry with the given key.

Finally, we call getToken tin the useEffect callback to run it when App mounts.

Therefore, we see 'secret' logged.

Conclusion

To store tokens in React Native, we can use the expo-secure-store package.

To install it, we run expo install expo-secure-store.

Categories
JavaScript Answers React Native Answers

How to fix ScrollView is not scrolling to the bottom sometimes issue with React Native?

To fix ScrollView is not scrolling to the bottom sometimes issue with React Native, we can set the contentContainerStyle to add some bottom padding.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <ScrollView
        contentContainerStyle={{ paddingBottom: 60 }}
        style={{ height: 300 }}>
        {Array(100)
          .fill()
          .map((_, i) => (
            <Text key={i}>{i}</Text>
          ))}
      </ScrollView>
    </View>
  );
}

to set contentContainerStyle to { paddingBottom: 60 } to add 60px of bottom padding on the ScrollView.

Anything inside the ScrollView will scroll.

Categories
JavaScript Answers React Native Answers

How to make image width 100 percent and vertical top with React Native?

Sometimes, we want to make image width 100 percent and vertical top with React Native.

In this article, we’ll look at how to make image width 100 percent and vertical top with React Native.

How to make image width 100 percent and vertical top with React Native?

To make image width 100 percent and vertical top with React Native, we can calculate the width and height of the image.

For instance, we write:

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

export default function App() {
  const dimensions = Dimensions.get('window');
  const imageHeight = Math.round((dimensions.width * 9) / 16);
  const imageWidth = dimensions.width;

  return (
    <View>
      <Image
        style={{ height: imageHeight, width: imageWidth }}
        source={{ uri: 'https://picsum.photos/200/300' }}
      />
    </View>
  );
}

to call Dimensions.get with 'window' to get the screen dimensions.

Then we set the imageHeight and imageWidth according to the screen dimensions.

We scale the imageHeight to keep the 16:9 aspect ratio for the image.

Conclusion

To make image width 100 percent and vertical top with React Native, we can calculate the width and height of the image.