Categories
React Native Answers

How to prevent rotation of screen with React Native?

To prevent rotation of screen with React Native, we can use the orientation field in your app.json file if our app is built with Expo.

For example, we write

{
  "expo": {
    "name": "My app",
    "slug": "my-app",
    "sdkVersion": "21.0.0",
    "privacy": "public",
    "orientation": "portrait"
  }
}

to set the orientation to portrait.

Categories
React Native Answers

How to add a progress bar slider with React Native?

To add a progress bar slider with React Native, we add the Slide component.

To use it, we write

import React, { Component } from 'react';

import { defaultString } from '../String/defaultStringValue';
import {
  View,
  Text,
  StyleSheet,
  Image,
  Slider,
  TouchableOpacity,
} from 'react-native';

function pad(n, width, z = 0) {
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

const minutesAndSeconds = (position) => ([
  pad(Math.floor(position / 60), 2),
  pad(position % 60, 2),
]);

const SeekBar = ({
  trackLength,
  currentPosition,
  onSeek,
  onSlidingStart,
}) => {
  const elapsed = minutesAndSeconds(currentPosition);
  const remaining = minutesAndSeconds(trackLength - currentPosition);
  return (
    <View style={styles.container}>
      <View style={{ flexDirection: 'row' }}>
        <Text style={[styles.text, { color: defaultString.darkColor }]}>
          {elapsed[0] + ":" + elapsed[1]}
        </Text>
        <View style={{ flex: 1 }} />
        <Text style={[styles.text, { width: 40, color: defaultString.darkColor }]}>
          {trackLength > 1 && "-" + remaining[0] + ":" + remaining[1]}
        </Text>
      </View>
      <Slider
        maximumValue={Math.max(trackLength, 1, currentPosition + 1)}
        onSlidingStart={onSlidingStart}
        onSlidingComplete={onSeek}
        value={currentPosition}
        minimumTrackTintColor={defaultString.darkColor}
        maximumTrackTintColor={defaultString.lightGrayColor}
        thumbStyle={styles.thumb}
        trackStyle={styles.track}
      />
    </View>
  );
};

export default SeekBar;

const styles = StyleSheet.create({
  slider: {
    marginTop: -12,
  },
  container: {
    paddingLeft: 16,
    paddingRight: 16,
    paddingTop: 16,
  },
  track: {
    height: 2,
    borderRadius: 1,
  },
  thumb: {
    width: 10,
    height: 10,
    borderRadius: 5,
    backgroundColor: defaultString.darkColor,
  },
  text: {
    color: 'rgba(255, 255, 255, 0.72)',
    fontSize: 12,
    textAlign: 'center',
  }
});


to add the Slider component into our view.

We set the maximumValue of the slide.

And we set various functions for the onSlidingStart and onSlidingComplete event handlers.

We also set the track color with minimumTrackTintColor and maximumTrackTintColor.

Categories
React Answers

How to get current language i18n-next React?

To get current language i18n-next React, we use the i18n.language.

For instance, we add

i18n.language

in our React app to get the current language.

Categories
React Answers

How to add react meta description with i18n-next React?

To add react meta description with i18n-next React, we can use the useIntl hook with React Helmet.

We install React Helmet with

npm i react-helmet

Then we use the useIntl hook to get the formatMessage function.

And then we add the meta tag into the Helmet component provided by React Helmet.

Then we set the content prop to a string returned by the f function to get the translated string by the id property.

import { useIntl } from 'react-intl';

const DescriptionMeta = () => {
  const { formatMessage: f } = useIntl();

  return (
    <Helmet>
      <meta name="description" content={f({ id: 'meta.description' })} />
    </Helmet>
  );
};
Categories
React Answers

How to turn off suspense with i18n-next React?

To turn off suspense with i18n-next React, we set the useSuspense option to false.

To do this, we write

i18n
  .use(XHR)
  .use(LanguageDetector)
  .init({
    react: { 
      useSuspense: false 
    }
});

to set useSuspense to false in the object we pass into the init method.