Categories
JavaScript Answers React Native Answers

How to see what’s stored in AsyncStorage with React Native?

Sometimes, we want to see what’s stored in AsyncStorage with React Native.

In this article, we’ll look at how to see what’s stored in AsyncStorage with React Native.

How to see what’s stored in AsyncStorage with React Native?

To see what’s stored in AsyncStorage with React Native, we can use the getAllKeys and multiGet methods.

For instance, we write:

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

export default function App() {
  const getEntries = async () => {
    await AsyncStorage.setItem('key', 'val');
    await AsyncStorage.setItem('key2', 'val');
    await AsyncStorage.setItem('key3', 'val');    
    const keys = await AsyncStorage.getAllKeys();
    const entries = await AsyncStorage.multiGet(keys);
    console.log(entries);
  };

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

  return <View></View>;
}

to call setItem with 3 key-value entries.

Then we call getAllKeys to return all entries for all the keys.

Next, we call multiGet with keys to return an array with the entries with the given keys.

And then we call getEntries in the useEffect callback to run it.

Conclusion

To see what’s stored in AsyncStorage with React Native, we can use the getAllKeys and multiGet methods.

Categories
JavaScript Answers React Native Answers

How to use a keyExtractor with FlatList with React Native?

Sometimes, we want to use a keyExtractor with FlatList with React Native.

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

How to use a keyExtractor with FlatList with React Native?

To use a keyExtractor with FlatList with React Native, we can set keyExtractor to a function that returns the unique ID value for each entry.

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>
      <FlatList
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to set keyExtractor to (item) => item.id to return the id property of a flatListItems entry.

Conclusion

To use a keyExtractor with FlatList with React Native, we can set keyExtractor to a function that returns the unique ID value for each entry.

Categories
JavaScript Answers React Native Answers

How to fix nested ScrollView locking up with React Native?

Sometimes, we want to fix nested ScrollView locking up with React Native.

In this article, we’ll look at how to fix nested ScrollView locking up with React Native.

How to fix nested ScrollView locking up with React Native?

To fix nested ScrollView locking up with React Native, we can set the nestedScrollEnabled prop to true.

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 nestedScrollEnabled={true} style={{ height: 300 }}>
        <ScrollView nestedScrollEnabled={true} style={{ height: 150 }}>
          {Array(100)
            .fill()
            .map((_, i) => (
              <Text key={i}>{i}</Text>
            ))}
        </ScrollView>
        <ScrollView nestedScrollEnabled={true} style={{ height: 150 }}>
          {Array(100)
            .fill()
            .map((_, i) => (
              <Text key={i}>{i}</Text>
            ))}
        </ScrollView>
      </ScrollView>
    </View>
  );
}

to add 2 ScrollViews in another ScrollView.

And we set the height of each so they all show on the screen.

We set nestedScrollEnabled to true so we can scroll the nested ScrollViews.

Conclusion

To fix nested ScrollView locking up with React Native, we can set the nestedScrollEnabled prop to true.

Categories
JavaScript Answers React Native Answers

How to fix AsyncStorage fetches data after rendering issue with React Native?

Sometimes, we want to fix AsyncStorage fetches data after rendering issue with React Native.

In this article, we’ll look at how to fix AsyncStorage fetches data after rendering issue with React Native.

How to fix AsyncStorage fetches data after rendering issue with React Native?

To fix AsyncStorage fetches data after rendering issue with React Native, we can check if the value is fetched before rendering them item we want.

For instance, we write:

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

export default function App() {
  const [val, setVal] = React.useState();

  const getData = async () => {
    await AsyncStorage.setItem('key', 'val');
    const v = await AsyncStorage.getItem('key');
    console.log(v);
    setVal(v);
  };

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

  return <View>{val && <Text>{val}</Text>}</View>;
}

to call AsyncStorage.getItem with the key to get the item with the given key.

Then we call setVal to set val to the returned value.

Next, we call useEffect with a callback that calls getData and an empty array to call getData when App mounts.

And then we check if val is set before rendering Text with

val && <Text>{val}</Text>

Conclusion

To fix AsyncStorage fetches data after rendering issue with React Native, we can check if the value is fetched before rendering them item we want.

Categories
JavaScript Answers React Native Answers

How to add border bottom in React Native?

Sometimes, we want to add border bottom in React Native.

In this article, we’ll look at how to add border bottom in React Native.

How to add border bottom in React Native?

To add border bottom in React Native, we can add border bottom on the View.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ borderBottom: '1px solid blue' }}>
      <Text>foo</Text>
    </View>
  );
}

to set borderBottom to '1px solid blue' on the View.

Now we see a bottom border on below the text and it’s blue.

Conclusion

To add border bottom in React Native, we can add border bottom on the View.