Categories
JavaScript Answers React Native Answers

How to get the value in TextInput when onBlur is called with React Native?

Sometimes, we want to get the value in TextInput when onBlur is called with React Native.

In this article, we’ll look at how to get the value in TextInput when onBlur is called with React Native.

How to get the value in TextInput when onBlur is called with React Native?

To get the value in TextInput when onBlur is called with React Native, we can get the value from the onEndEditing callback.

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
        onEndEditing={(e) => console.log(e.nativeEvent.text)}
        placeholder="Name"
      />
    </View>
  );
}

to set onEndEditing to (e) => console.log(e.nativeEvent.text) after we press enter.

Conclusion

To get the value in TextInput when onBlur is called with React Native, we can get the value from the onEndEditing callback.

Categories
JavaScript Answers React Native Answers

How to add space between two elements on the same line in React Native?

Sometimes, we want to add space between two elements on the same line in React Native.

In this article, we’ll look at how to add space between two elements on the same line in React Native.

How to add space between two elements on the same line in React Native?

To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View.

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 style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
      <Text>foo</Text>
      <Text>bar</Text>
    </View>
  );
}

to set the flexDirection to 'row' and the justifyContent style to 'space-between' to spread the Text components on the screen.

As a result, we should see ‘foo’ displayed on the left side and ‘bar’ on the right.

Conclusion

To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View.

Categories
JavaScript Answers React Native Answers

How to disable iOS keyboard suggestions in React Native?

Sometimes, we want to disable iOS keyboard suggestions in React Native.

In this article, we’ll look at how to disable iOS keyboard suggestions in React Native.

How to disable iOS keyboard suggestions in React Native?

To disable iOS keyboard suggestions in React Native, we can set the autoCorrect prop to false.

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 autoCorrect={false} />
    </View>
  );
}

to set autoCorrect to false.

Now the keyboard suggestions should be hidden in iOS.

Conclusion

To disable iOS keyboard suggestions in React Native, we can set the autoCorrect prop to false.

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.