Categories
JavaScript Answers React Native Answers

How to capitalize first letter of text in React Native?

Sometimes, we want to capitalize first letter of text in React Native.

In this article, we’ll look at how to capitalize first letter of text in React Native.

How to capitalize first letter of text in React Native?

To capitalize first letter of text in React Native, we can create our own function.

For instance, we write:

import * as React from 'react';
import { Text, View } from 'react-native';

const capitalize = (str) => {
  return str[0].toUpperCase() + str.slice(1);
};

export default function App() {
  return (
    <View>
      <Text>{capitalize('hello')}</Text>
    </View>
  );
}

to define the capitalize function that returns a string with the first letter of str capitalized.

We call toUpperCase to capitalize the first letter and slice with 1 to return the characters in str starting from index 1 to the end of the string.

As a result, we see ‘Hello’ displayed.

Conclusion

To capitalize first letter of text in React Native, we can create our own function.

Categories
JavaScript Answers React Native Answers

How to disable a View component in React Native?

Sometimes, we want to disable a View component in React Native.

In this article, we’ll look at how to disable a View component in React Native.

How to disable a View component in React Native?

To disable a View component in React Native, we can set the pointerEvents prop to 'none'.

For instance, we write:

import * as React from 'react';
import { Text, View } from 'react-native';

export default function App() {
  return (
    <View>
      <View pointerEvents="none">...</View>
    </View>
  );
}

to set the inner view’s pointerEvents prop to 'none' so that users can’t interact with the content inside.

Conclusion

To disable a View component in React Native, we can set the pointerEvents prop to 'none'.

Categories
JavaScript Answers React Native Answers

How to vertically align image with resizeMode “contain” with React Native?

Sometimes, we want to vertically align image with resizeMode "contain" with React Native.

In this article, we’ll look at how to vertically align image with resizeMode "contain" with React Native.

How to vertically align image with resizeMode "contain" with React Native?

To vertically align image with resizeMode "contain" with React Native, we can use justifyContent.

For instance, we write:

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

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

to set justifyContent to 'center' to center align the Image vertically.

We also set the image height and width to make it display.

As a result, we should see the image display vertically centered.

Conclusion

To vertically align image with resizeMode "contain" with React Native, we can use justifyContent.

Categories
JavaScript Answers React Native Answers

How to fix textAlign: ‘right’ not styling correctly with React Native?

Sometimes, we want to fix textAlign: ‘right’ not styling correctly with React Native.

In this article, we’ll look at how to fix textAlign: ‘right’ not styling correctly with React Native.

How to fix textAlign: ‘right’ not styling correctly with React Native?

To fix textAlign: ‘right’ not styling correctly with React Native, we can set textAlign to 'right' on the Text component in addition to using flexbox.

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 style={{ flex: 1, flexDirection: 'row' }}>
      <View style={{ flex: 1 }}>
        <Text>100 Views 0 Comments</Text>
      </View>
      <View style={{ flex: 1 }}>
        <Text style={{ textAlign: 'right' }}>View</Text>
      </View>
    </View>
  );
}

to set flexDirection to 'row' to let us display the inner views side by side.

Then we set textAlign to 'right' on the 2nd Text component to align the text of that on the right.

Conclusion

To fix textAlign: ‘right’ not styling correctly with React Native, we can set textAlign to 'right' on the Text component in addition to using flexbox.

Categories
JavaScript Answers React Native Answers

How to change the React Native text input cursor color?

Sometimes, we want to change the React Native text input cursor color.

In this article, we’ll look at how to change the React Native text input cursor color.

How to change the React Native text input cursor color?

To change the React Native text input cursor color, we can set the selectionColor prop to the cursor color.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <TextInput selectionColor="green" placeholder="name" />
    </View>
  );
}

to set selectionColor to 'green' to set the cursor color on the text input to green.

Conclusion

To change the React Native text input cursor color, we can set the selectionColor prop to the cursor color.