Categories
JavaScript Answers React Native Answers

How to get TextInput value with React Native?

Sometimes, we want to get TextInput value with React Native.

In this article, we’ll look at how to get TextInput value with React Native.

How to get TextInput value with React Native?

To get TextInput value with React Native, we can get it from the onChangeText callback.

For instance, we write:

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

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

  return (
    <View>
      <TextInput
        onChangeText={(username) => setUsername(username)}
        value={username}
      />
      <Text>{username}</Text>
    </View>
  );
}

to set the onChangeText prop of the TextInput to a function that takes the input value as the argument.

In the function, we call setUsername to set the username value to the TextInput‘s value.

Therefore, when we type into the input, we see the same value displayed in the Text component.

Conclusion

To get TextInput value with React Native, we can get it from the onChangeText callback.

Categories
JavaScript Answers React Native Answers

How to maintain aspect ratio of image with full width in React Native?

Sometimes, we want to maintain aspect ratio of image with full width in React Native.

In this article, we’ll look at how to maintain aspect ratio of image with full width in React Native.

How to maintain aspect ratio of image with full width in React Native?

To maintain aspect ratio of image with full width in React Native, we can set the aspectRatio style property.

For instance, we write:

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

export default function App() {
  const win = Dimensions.get('window');
  const ratio = win.width / 200;

  return (
    <View>
      <Image
        source="https://picsum.photos/200/300"
        style={{
          width: '100%',
          height: undefined,
          aspectRatio: 135 / 76,
        }}
      />
    </View>
  );
}

to add an Image with the width set to '100%'.

And we set the height to undefined to keep the aspect ratio and set the aspectRatio property to set the aspect ratio of the image.

Conclusion

To maintain aspect ratio of image with full width in React Native, we can set the aspectRatio style property.

Categories
JavaScript Answers React Native Answers

How to use setTimeout in React Native?

Sometimes, we want to use setTimeout in React Native.

In this article, we’ll look at how to use setTimeout in React Native.

How to use setTimeout in React Native?

To use setTimeout in React Native, we can call it directly.

For instance, we write:

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

export default function App() {
  const [loaded, setLoaded] = React.useState(false);
  React.useEffect(() => {
    setTimeout(() => {
      setLoaded(true);
    }, 3000);
  }, []);

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

We call setTimeout with a callback that calls setLoaded to set loaded to true in 3000 milliseconds.

Then we show ‘loaded’whenloadedistrue`.

Therefore, ‘loaded’ is shown after 3 seconds.

Conclusion

To use setTimeout in React Native, we can call it directly.

Categories
JavaScript Answers React Native Answers

How to set image width to be 100% and height to be auto in React Native?

To set image width to be 100% and height to be auto in React Native, we can set the width and height of the Image.

For instance, we write:

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

export default function App() {
  const win = Dimensions.get('window');
  const ratio = win.width / 200;

  return (
    <View
      style={{
        flexGrow: 1,
      }}>
      <Image
        source="https://picsum.photos/200/300"
        style={{
          width: win.width,
          height: 300 * ratio,
        }}
      />
    </View>
  );
}

to call Dimensions.get with 'window' to get the window’s dimension.

Then we calculate the ratio between the width and height of the image with win.width / 200, where 200 is the width of the image.

Then we set the width to win.width and the height to 300 * ratio where 300 is the height of the image.can set the width and height of the Image.

Categories
JavaScript Answers React Native Answers

How to do vertical centering when using ScrollView with React Native?

Sometimes, we want to do vertical centering when using ScrollView with React Native.

In this article, we’ll look at how to do vertical centering when using ScrollView with React Native.

How to do vertical centering when using ScrollView with React Native?

To do vertical centering when using ScrollView with React Native, we can set the contentContainerStyle prop to an object with some flexbox styles.

For instance, we write:

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

export default function App() {
  return (
    <View
      style={{
        flexGrow: 1,
      }}>
      <ScrollView
        contentContainerStyle={{
          flexGrow: 1,
          justifyContent: 'center',
          flexDirection: 'column',
        }}>
        {Array(200)
          .fill()
          .map((_, i) => {
            return <Text>{i}</Text>;
          })}
      </ScrollView>
    </View>
  );
}

We set the contentContainerStyle prop to:

{
  flexGrow: 1,
  justifyContent: 'center',
  flexDirection: 'column',
}

to expand the ScrollView to fit the View.

And we set justifyContent to 'center' to center align the ScrollView.

We set the flexDirection to 'column' to vertically justify items.

Conclusion

To do vertical centering when using ScrollView with React Native, we can set the contentContainerStyle prop to an object with some flexbox styles.