Categories
JavaScript Answers React Native Answers

How to change background color of React Native button?

Sometimes, we want to change background color of React Native button.

In this article, we’ll look at how to change background color of React Native button.

How to change background color of React Native button?

To change background color of React Native button, we can set the color prop for Android and we set the backgroundColor for iOS.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <Button title="Login Android" color="#1E6738" />
      <TouchableOpacity style={{ backgroundColor: '#1E6738' }}>
        <Text style={{ color: '#fff', textAlign: 'center' }}>Login iOS</Text>
      </TouchableOpacity>
    </View>
  );
}

to add <Button title="Login Android" color="#1E6738" /> add a button with the background color set to #1E6738 for Android.

Then for iOS, we write set the backgroundColor on the TouchableOpacity component to add the background color.

And we set the text color in the Text component.

Conclusion

To change background color of React Native button, we can set the color prop for Android and we set the backgroundColor for iOS.

Categories
JavaScript Answers React Native Answers

How to centralize text in React Native TextInput?

Sometimes, we want to centralize text in React Native TextInput.

In this article, we’ll look at how to centralize text in React Native TextInput.

How to centralize text in React Native TextInput?

To centralize text in React Native TextInput, we can set the textAlign prop to 'center'.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ padding: 30 }}>
      <TextInput textAlign="center" placeholder="Your Account" />
    </View>
  );
}

to set the textAlign prop to 'center' to make the placeholder and input value centered on the screen.

Conclusion

To centralize text in React Native TextInput, we can set the textAlign prop to 'center'.

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.