Categories
JavaScript Answers React Native Answers

How to add space between text lines in React Native?

Sometimes, we want to add space between text lines in React Native.

In this article, we’ll look at how to add space between text lines in React Native.

How to add space between text lines in React Native?

To add space between text lines in React Native, we can set the lineHeight style.

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>
      {Array(10)
        .fill()
        .map((_, i) => {
          return <Text style={{ lineHeight: 30 }}>{i}</Text>;
        })}
    </View>
  );
}

to render to Text components with the lineHeight of each set to 30 pixels.

As a result, we should see extra space between each line of text.

Conclusion

To add space between text lines in React Native, we can set the lineHeight style.

Categories
JavaScript Answers React Native Answers

How to fix ScrollView Not scrolling with React Native?

To fix ScrollView Not scrolling with React Native, we wrap the content of the ScrollView with the ScrollView.

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={{ flex: 1 }}>
      <ScrollView>
        {Array(200)
          .fill()
          .map((_, i) => {
            return <Text>{i}</Text>;
          })}
      </ScrollView>
    </View>
  );
}

to wrap ScrollView around the Text components that we render inside.

Anything inside the ScrollView will scroll.

As a result, we should see text inside and we can scroll up and down.

Categories
JavaScript Answers React Native Answers

How to style the standard React Native Android picker?

Sometimes, we want to style the standard React Native Android picker.

In this article, we’ll look at how to style the standard React Native Android picker.

How to style the standard React Native Android picker?

To style the standard React Native Android picker, we can set the color prop on Picker.Item.

For instance, we write:

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

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

  return (
    <View style={{ padding: 30 }}>
      <Picker selectedValue={val} onValueChange={setVal} mode="dropdown">
        <Picker.Item label="hello" value="0" color="blue" />
        <Picker.Item label="world" value="1" color="blue" />
      </Picker>
    </View>
  );
}

to add a Picker.

Then we set the color prop of the Picker.Item to blue to make the drop down text blue.

Conclusion

To style the standard React Native Android picker, we can set the color prop on Picker.Item.

Categories
JavaScript Answers React Native Answers

How to use Image with a local file with React Native?

Sometimes, we want to use Image with a local file with React Native.

In this article, we’ll look at how to use Image with a local file with React Native.

How to use Image with a local file with React Native?

To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop’s value.

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() {
  const [text, setText] = React.useState();

  return (
    <View>
      <Image source={require('./assets/snack-icon.png')} />
    </View>
  );
}

to call require with a local image path.

And then we set that as the source value to display the image.

Conclusion

To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop’s value.

Categories
JavaScript Answers React Native Answers

How to set max length of the TextInput with React Native?

Sometimes, we want to set max length of the TextInput with React Native.

In this article, we’ll look at how to set max length of the TextInput with React Native.

How to set max length of the TextInput with React Native?

To set max length of the TextInput with React Native, we can set the maxLength prop.

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 [text, setText] = React.useState();

  return (
    <View>
      <TextInput value={text} onChangeText={setText} maxLength={10} />
    </View>
  );
}

to set the maxLength prop to 10 to allow users to enter a max of 10 characters into the input.

Conclusion

To set max length of the TextInput with React Native, we can set the maxLength prop.