Categories
JavaScript Answers React Native Answers

How to make component stick to bottom in ScrollView but still allow other content to push it down with React Native?

Sometimes, we want to make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

In this article, we’ll look at how to make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

How to make component stick to bottom in ScrollView but still allow other content to push it down with React Native?

To make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

For instance, we write:

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

export default function App() {
  return (
    <ScrollView
      contentContainerStyle={{
        flexGrow: 1,
        flexDirection: 'column',
        justifyContent: 'space-between',
      }}>
      <View style={{ width: 50, height: 1000, backgroundColor: 'orange' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'black' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'blue' }} />
    </ScrollView>
  );
}

to add 3 Views that has different heights.

They’ll be display on top of each other since we set flexDirection to 'column'.

Conclusion

To make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

Categories
JavaScript Answers React Native Answers

How to add text like an HTML span with React Native?

Sometimes, we want to add text like an HTML span with React Native.

In this article, we’ll look at how to add text like an HTML span with React Native.

How to add text like an HTML span with React Native?

To add text like an HTML span with React Native, we can nest Text components.

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>
        <Text>foo</Text>
        <Text>bar</Text>
      </Text>
    </View>
  );
}

to add 2 Text components beside each other in an outer Text component.

Therefore, we see ‘foobar’ on the screen since they’re displayed inline.

Conclusion

To add text like an HTML span with React Native, we can nest Text components.

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.