Categories
JavaScript Answers React Native Answers

How to get the coordinates of a touch event with React Native?

Sometimes, we want to get the coordinates of a touch event with React Native.

In this article, we’ll look at how to get the coordinates of a touch event with React Native.

How to get the coordinates of a touch event with React Native?

To get the coordinates of a touch event with React Native, we can get it from the touch event object.

For instance, we write:

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

export default function App() {
  const onPress = (evt) => {
    console.log(evt.nativeEvent.locationX, evt.nativeEvent.locationY);
  };

  return (
    <View
      style={{
        height: '100%',
        width: '100%',
        justifyContent: 'center',
        alignItems: 'center',
      }}>
      <TouchableOpacity onPress={onPress}>
        <Text>hello world</Text>
      </TouchableOpacity>
    </View>
  );
}

to set the TouchableOpacity‘s onPress prop to onPress.

Then we get the x and y coordinate of the touch from the evt.nativeEvent.locationX and locationY properties.

Conclusion

To get the coordinates of a touch event with React Native, we can get it from the touch event object.

Categories
JavaScript Answers React Native Answers

How to add horizontal ScrollView snapping with React Native?

Sometimes, we want to add horizontal ScrollView snapping with React Native.

In this article, we’ll look at how to add horizontal ScrollView snapping with React Native.

How to add horizontal ScrollView snapping with React Native?

To add horizontal ScrollView snapping with React Native, we can set the snapToAlignment prop to 'center'.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <ScrollView
        horizontal
        decelerationRate={0}
        snapToInterval={200}
        snapToAlignment="center">
        {Array(100)
          .fill()
          .map((_, i) => (
            <Text
              key={i}
              style={{
                width: 100,
                height: 100,
                backgroundColor: 'green',
                margin: 10,
              }}>
              {i}
            </Text>
          ))}
      </ScrollView>
    </View>
  );
}

to add the horizontal prop to make the ScrollView scroll horizontally.

And we set snapToAlignment to 'center' to make the ScrollView snap element in the center of the screen.

Conclusion

To add horizontal ScrollView snapping with React Native, we can set the snapToAlignment prop to 'center'.

Categories
JavaScript Answers React Native Answers

How to add email validation with React Native?

Sometimes, we want to add email validation with React Native.

In this article, we’ll look at how to add email validation with React Native.

How to add email validation with React Native?

To add email validation with React Native, we can use the regex test method.

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() {
  const validate = (text) => {
    const reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;
    console.log(text, reg.test(text));
  };

  return (
    <View>
      <TextInput placeholder="Email" onChangeText={validate} />
    </View>
  );
}

to create the validate function that calls reg.test with the text to validate.

reg is the pattern for emails.

We check if text is a string with groups of characters separated by periods combined with an @.

Then we set the onChangeText prop to validate to validate the email as we type.

Conclusion

To add email validation with React Native, we can use the regex test method.

Categories
JavaScript Answers React Native Answers

How to change TextInput placeHolder alignment with React Native?

Sometimes, we want to change TextInput placeHolder alignment with React Native

In this article, we’ll look at how to change TextInput placeHolder alignment with React Native.

How to change TextInput placeHolder alignment with React Native?

To change TextInput placeHolder alignment with React Native, we can set the textAlign style to 'center'.

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>
      <TextInput placeholder="Name" style={{ textAlign: 'center' }} />
    </View>
  );
}

to align the placeholder at the center of the screen by setting textAlign set to 'center'.

Conclusion

To change TextInput placeHolder alignment with React Native, we can set the textAlign style to 'center'.

Categories
JavaScript Answers React Native Answers

How to avoid text wrapping with React Native?

Sometimes, we want to avoid text wrapping with React Native.

In this article, we’ll look at how to avoid text wrapping with React Native.

How to avoid text wrapping with React Native?

To avoid text wrapping with React Native, we can set the numberOfLines prop to 1.

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={{ flexDirection: 'row' }}>
      <Text numberOfLines={1} style={{ flex: 1, textAlign: 'left' }}>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      </Text>
    </View>
  );
}

to set the numberOfLines prop of the Text component to 1 to show the text in 1 line and truncate the part that can’t be shown in 1 line.

Conclusion

To avoid text wrapping with React Native, we can set the numberOfLines prop to 1.