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.

Categories
JavaScript Answers React Native Answers

How to set the focus style for TextInput in React Native?

Sometimes, we want to set the focus style for TextInput in React Native.

In this article, we’ll look at how to set the focus style for TextInput in React Native.

How to set the focus style for TextInput in React Native?

To set the focus style for TextInput in React Native, we can set the style in the focus and blur handlers.

For instance, we write:

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

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

  return (
    <View>
      <TextInput
        onBlur={() => setBackgroundColor('yellow')}
        onFocus={() => setBackgroundColor('orange')}
        style={{
          backgroundColor,
        }}
        placeholder="Name"
      />
    </View>
  );
}

to set the onBlur and onFocus props to the blur and focus event handlers.

onBlur is called when the text input loses focus.

In the handler functions, we call setBackgroundColor to the background color of the text inputs.

And we set the style prop to the backgroundColor.

As a result, we see the background colors applied when we focus on and away from the text input.

Conclusion

To set the focus style for TextInput in React Native, we can set the style in the focus and blur handlers.

Categories
JavaScript Answers React Native Answers

How to add image and onPress into touchable with React Native?

Sometimes, we want to add image and onPress into touchable with React Native.

In this article, we’ll look at how to add image and onPress into touchable with React Native.

How to add image and onPress into touchable with React Native?

To add image and onPress into touchable with React Native, we can set the onPress prop on the TouchableHighlight and put the Image the TouchableHighlight.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <TouchableHighlight onPress={() => console.log('pressed')}>
        <Image
          source={{ uri: 'https://picsum.photos/200/300' }}
          style={{ height: 300, width: 200 }}
        />
      </TouchableHighlight>
    </View>
  );
}

to set onPress to a function that logs 'pressed'.

And we put the Image inside.

Asa a result, we see 'pressed' logged when we click on the image.

Conclusion

To add image and onPress into touchable with React Native, we can set the onPress prop on the TouchableHighlight and put the Image the TouchableHighlight.

Categories
JavaScript Answers React Native Answers

How to scroll to end of FlatList after displaying the keyboard with React Native?

Sometimes, we want to scroll to end of FlatList after displaying the keyboard with React Native.

In this article, we’ll look at how to scroll to end of FlatList after displaying the keyboard with React Native.

How to scroll to end of FlatList after displaying the keyboard with React Native?

To scroll to end of FlatList after displaying the keyboard with React Native, we can set the onLayour prop to a function that calls scrollToEnd on the FlatList’s ref.

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} />;
  const flatListRef = React.useRef();

  return (
    <View style={{ height: 300 }}>
      <FlatList
        ref={flatListRef}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
        onLayout={() => flatListRef.current.scrollToEnd({ animated: true })}
      />
    </View>
  );
}

to set onLayout to a function that calls flatListRef.current.scrollToEnd to scroll the FlatList to the end.

We create the ref with useRef and assigned it as the value of the FlatList‘s ref prop.

Conclusion

To scroll to end of FlatList after displaying the keyboard with React Native, we can set the onLayour prop to a function that calls scrollToEnd on the FlatList’s ref.