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.

Categories
JavaScript Answers React Native Answers

How to upload a photo with Expo?

Sometimes, we want to upload a photo with Expo.

In this article, we’ll look at how to upload a photo with Expo.

How to upload a photo with Expo?

To upload a photo with Expo, we can use the expo-image-picker package.

We install it by running npm i expo-image-picker.

Then we use it by writing:

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

const takeAndUploadPhotoAsync = async () => {
  let result = await ImagePicker.launchCameraAsync({
    allowsEditing: true,
    aspect: [4, 3],
  });

  if (result.cancelled) {
    return;
  }
  let localUri = result.uri;
  let filename = localUri.split('/').pop();
  let match = /\.(\w+)$/.exec(filename);
  let type = match ? `image/${match[1]}` : `image`;

  let formData = new FormData();
  formData.append('photo', { uri: localUri, name: filename, type });

  return await fetch('https://https://jsonplaceholder.typicode.com/', {
    method: 'POST',
    body: formData,
    headers: {
      'content-type': 'multipart/form-data',
    },
  });
};
export default function App() {
  React.useEffect(() => {
    takeAndUploadPhotoAsync();
  }, []);

  return <View></View>;
}

to define the takeAndUploadPhotoAsync that calls ImagePicker.launchCameraAsync to start the camera.

Then we get the result after the picture is taken with result.uri.

Next, we get the filename with localUri.split('/').pop().

And then we get the MIME type with match ? `image/${match[1]}` : `image`.

Next, we create a new FormData instance and append the photo to that.

And then we call fetch to submit the FormData instance.

Conclusion

To upload a photo with Expo, we can use the expo-image-picker package.

We install it by running npm i expo-image-picker.

Categories
JavaScript Answers React Native Answers

How to adjust font size to fit view in React Native for Android?

Sometimes, we want to adjust font size to fit view in React Native for Android.

In this article, we’ll look at how to adjust font size to fit view in React Native for Android.

How to adjust font size to fit view in React Native for Android?

To adjust font size to fit view in React Native for Android, we can adjust the font size to fit the text onto the number of lines.

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';

const AdjustLabel = ({ fontSize, text, style, numberOfLines }) => {
  const [currentFont, setCurrentFont] = React.useState(fontSize);

  return (
    <Text
      numberOfLines={numberOfLines}
      adjustsFontSizeToFit
      style={[style, { fontSize: currentFont }]}
      onTextLayout={(e) => {
        const { lines } = e.nativeEvent;
        if (lines.length > numberOfLines) {
          setCurrentFont(currentFont - 1);
        }
      }}>
      {text}
    </Text>
  );
};

export default function App() {
  return (
    <View style={{ overflow: 'hidden', paddingBottom: 5 }}>
      <AdjustLabel
        text="Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        fontSize={50}
        numberOfLines={2}
      />
    </View>
  );
}

to create the AdjustLabel component that has the adjustsFontSizeToFit prop and also calculates the font size in the onTextLayout callback.

We calculate the font size in the callback by getting lines.length from the event.

Then we adjust the font if lines.length is bigger than numberOfLines.

And we set the fontSize to currentFont to change the font size.

Conclusion

To adjust font size to fit view in React Native for Android, we can adjust the font size to fit the text onto the number of lines.