Categories
JavaScript Answers React Native Answers

How to remove underline in TextInput in React Native?

Sometimes, we want to remove underline in TextInput in React Native.

In this article, we’ll look at how to remove underline in TextInput in React Native.

How to remove underline in TextInput in React Native?

To remove underline in TextInput in React Native, we can set the underlineColorAndroid prop to transparent.

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() {
  return (
    <View style={{ padding: 30 }}>
      <TextInput underlineColorAndroid="transparent" placeholder="Name" />
    </View>
  );
}

to set the underlineColorAndroid prop of the TextInput to 'transparent' to remove the TextInput underline.

Conclusion

To remove underline in TextInput in React Native, we can set the underlineColorAndroid prop to transparent.

Categories
JavaScript Answers React Native Answers

How to fit an Image to the full width of the screen and maintain aspect ratio with React Native?

Sometimes, we want to fit an Image to the full width of the screen and maintain aspect ratio with React Native.

In this article, we’ll look at how to fit an Image to the full width of the screen and maintain aspect ratio with React Native.

How to fit an Image to the full width of the screen and maintain aspect ratio with React Native?

To fit an Image to the full width of the screen and maintain aspect ratio with React Native, we can set the width to null and the height to the height we want.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <Image
        style={{
          height: 300,
          flex: 1,
          width: null,
        }}
        source={{ uri: 'https://picsum.photos/200/100' }}
      />
    </View>
  );
}

to set the height of the Image to 300 and the width to null to fit the Image to make the width flexible and maintain aspect ratio.

We set flex to 1 to make stretch the Image to the width of the screen.

Conclusion

To fit an Image to the full width of the screen and maintain aspect ratio with React Native, we can set the width to null and the height to the height we want.

Categories
JavaScript Answers React Native Answers

How to get the current page in FlatList when using pagingEnabled with React-Native?

Sometimes, we want to get the current page in FlatList when using pagingEnabled with React-Native.

In this article, we’ll look at how to get the current page in FlatList when using pagingEnabled with React-Native.

How to get the current page in FlatList when using pagingEnabled with React-Native?

To get the current page in FlatList when using pagingEnabled with React-Native, we can calculate it from the scroll distance.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View, Dimensions } 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 keyExtractor = (item) => item.id;
  const dimensions = Dimensions.get('window');
  const onMomentumScrollEnd = (e) => {
    const pageNumber = Math.min(
      Math.max(
        Math.floor(e.nativeEvent.contentOffset.x / dimensions.width + 0.5) + 1,
        0
      ),
      flatListItems.length
    );
    console.log(pageNumber);
  };

  return (
    <View style={{ height: 300 }}>
      <FlatList
        horizontal
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        onMomentumScrollEnd={onMomentumScrollEnd}
      />
    </View>
  );
}

to define the onMomentumScrollEnd which gets the width of the screen with Dimensions.get.

Then we get the page number with

const pageNumber = Math.min(
  Math.max(
    Math.floor(e.nativeEvent.contentOffset.x / dimensions.width + 0.5) + 1,
    0
  ),
  flatListItems.length
)

to divide the horizontal scroll distance e.nativeEvent.contentOffset.x by dimensions.width.

Now when we stop scrolling, we should see the page number logged.

Conclusion

To get the current page in FlatList when using pagingEnabled with React-Native, we can calculate it from the scroll distance.

Categories
JavaScript Answers React Native Answers

How to get current URL with React Native webview?

Sometimes, we want to get current URL with React Native webview.

In this article, we’ll look at how to get current URL with React Native webview.

How to get current URL with React Native webview?

To get current URL with React Native webview, we can set the onNavigationStateChange prop to a function that listens for navigation events in the webview.

For instance, we write:

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

export default function App() {
  const onNavigationStateChange = (webViewState) => {
    console.log(webViewState.url);
  };

  return (
    <View style={{ flex: 1 }}>
      <WebView
        source={{ uri: 'https://example.com' }}
        onNavigationStateChange={onNavigationStateChange}
        javaScriptEnabled
        domStorageEnabled
        startInLoadingState={false}
      />
    </View>
  );
}

to set onNavigationStateChange to the onNavigationStateChange function.

In it, we log webViewState.url which has the URL of the current page in the webview.

Conclusion

To get current URL with React Native webview, we can set the onNavigationStateChange prop to a function that listens for navigation events in the webview.

Categories
JavaScript Answers React Native Answers

How to fix React Native text getting vertically cut off?

Sometimes, we want to fix React Native text getting vertically cut off.

In this article, we’ll look at how to fix React Native text getting vertically cut off.

How to fix React Native text getting vertically cut off?

To fix React Native text getting vertically cut off, we should add padding to the Text component.

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>
      <Text style={{ padding: 15 }}>text</Text>
    </View>
  );
}

to set the padding style to 15px on the Text component.

Now the text shouldn’t be cut off.

Conclusion

To fix React Native text getting vertically cut off, we should add padding to the Text component.