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.

Categories
JavaScript Answers React Native Answers

How to set elevation shadow only on the bottom on React Native?

Sometimes, we want to set elevation shadow only on the bottom on React Native.

In this article, we’ll look at how to set elevation shadow only on the bottom on React Native.

How to set elevation shadow only on the bottom on React Native?

To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden'.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ overflow: 'hidden', paddingBottom: 5 }}>
      <View
        style={{
          backgroundColor: '#fff',
          width: 300,
          height: 60,
          shadowColor: '#000',
          shadowOffset: { width: 1, height: 1 },
          shadowOpacity: 0.4,
          shadowRadius: 3,
          elevation: 5,
        }}
      />
    </View>
  );
}

to set overflow to 'hidden' on the outer View.

And then we add the shadow styles in the inner view to add the shadow.

elevation is needed for Android to show the shadow.

Conclusion

To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden'.

Categories
JavaScript Answers React Native Answers

How to make component stick to bottom in ScrollView but still allow other content to push it down with React Native?

Sometimes, we want to make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

In this article, we’ll look at how to make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

How to make component stick to bottom in ScrollView but still allow other content to push it down with React Native?

To make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

For instance, we write:

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

export default function App() {
  return (
    <ScrollView
      contentContainerStyle={{
        flexGrow: 1,
        flexDirection: 'column',
        justifyContent: 'space-between',
      }}>
      <View style={{ width: 50, height: 1000, backgroundColor: 'orange' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'black' }} />
      <View style={{ width: 50, height: 50, backgroundColor: 'blue' }} />
    </ScrollView>
  );
}

to add 3 Views that has different heights.

They’ll be display on top of each other since we set flexDirection to 'column'.

Conclusion

To make component stick to bottom in ScrollView but still allow other content to push it down with React Native.

Categories
JavaScript Answers React Native Answers

How to add text like an HTML span with React Native?

Sometimes, we want to add text like an HTML span with React Native.

In this article, we’ll look at how to add text like an HTML span with React Native.

How to add text like an HTML span with React Native?

To add text like an HTML span with React Native, we can nest Text components.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <Text>
        <Text>foo</Text>
        <Text>bar</Text>
      </Text>
    </View>
  );
}

to add 2 Text components beside each other in an outer Text component.

Therefore, we see ‘foobar’ on the screen since they’re displayed inline.

Conclusion

To add text like an HTML span with React Native, we can nest Text components.