Categories
JavaScript Answers React Native Answers

How to show current time and update the seconds in real time with React Native?

Sometimes, we want to show current time and update the seconds in real time with React Native.

In this article, we’ll look at how to show current time and update the seconds in real time with React Native.

How to show current time and update the seconds in real time with React Native?

To show current time and update the seconds in real time with React Native, we can use the setInterval function to update the time every second.

For instance, we write:

import * as React from 'react';
import { Text, View } from 'react-native';


export default function App() {
  const [time, setTime] = React.useState();

  React.useEffect(() => {
    const timer = setInterval(() => {
      setTime(new Date().toLocaleString());
    }, 1000);

    return () => {
      clearInterval(timer);
    };
  }, []);

  return (
    <View>
      <Text>{time}</Text>
    </View>
  );
}

to call setInterval with a callback that calls setTime with new Date().toLocaleString() to set time to the current date time string.

The 2nd argument of setInterval is 1000 so the callback runs every second.

Also, we return a function that calls clearInterval to clear the timer when we unmount the component.

The useEffect hook is called with an empty array so the useEffect callback runs only when App mounts.

Finally, we display the time in the Text component.

Conclusion

To show current time and update the seconds in real time with React Native, we can use the setInterval function to update the time every second.

Categories
JavaScript Answers React Native Answers

How to show 2 items per row with React Native?

Sometimes, we want to show 2 items per row with React Native.

In this article, we’ll look at how to show 2 items per row with React Native.

How to show 2 items per row with React Native?

To show 2 items per row with React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.

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 style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
      {Array(100)
        .fill()
        .map((_, i) => {
          return (
            <View style={{ width: '50%' }}>
              <Text>{i}</Text>
            </View>
          );
        })}
    </View>
  );
}

to set flexDirection and flexWrap so we get a horizontal flex layout and we wrap overflowing components to the next row.

Then we set each View‘s width to 50% so that they take half the width of the screen.

Conclusion

To show 2 items per row with React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.

Categories
JavaScript Answers React Native Answers

How to enable or disable scrolling on FlatList with React Native?

Sometimes, we want to enable or disable scrolling on FlatList with React Native.

In this article, we’ll look at how to enable or disable scrolling on FlatList with React Native.

How to enable or disable scrolling on FlatList with React Native?

To enable or disable scrolling on FlatList with React Native, we can set the scrollEnabled prop.

For instance, we write:

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

const flatListItems = Array(200)
  .fill()
  .map((_, i) => ({ title: i, id: i }));

const Item = ({ title }) => (
  <View
    style={{
      backgroundColor: '#f9c2ff',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
    }}>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;
  const keyExtractor = (item) => item.id;

  return (
    <View>
      <FlatList
        scrollEnabled={false}
        style={{ height: 300 }}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      />
    </View>
  );
}

to set the scrollEnabled prop to false to disable scrolling on the FlatList.

Conclusion

To enable or disable scrolling on FlatList with React Native, we can set the scrollEnabled prop.

Categories
JavaScript Answers React Native Answers

How to fix textDecoration properties not working on Android with React Native?

Sometimes, we want to fix textDecoration properties not working on Android with React Native.

In this article, we’ll look at how to fix textDecoration properties not working on Android with React Native.

How to fix textDecoration properties not working on Android with React Native?

To fix textDecoration properties not working on Android with React Native, we can set the textDecorationLine, textDecorationStyle and textDecorationColor styles.

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
        style={{
          textDecorationLine: 'underline',
          textDecorationStyle: 'solid',
          textDecorationColor: '#000',
        }}>
        hello
      </Text>
    </View>
  );
}

to set textDecorationLine to 'underline' to add text underline.

textDecorationStyle set to 'solid' makes the underline solid.

textDecorationColor set the underline color.

Conclusion

To fix textDecoration properties not working on Android with React Native, we can set the textDecorationLine, textDecorationStyle and textDecorationColor styles.

Categories
JavaScript Answers React Native Answers

How to add a 2 column layout with flexbox on React Native?

Somwtimes, we want to add a 2 column layout with flexbox on React Native.

In this article, we’ll look at how to add a 2 column layout with flexbox on React Native.

How to add a 2 column layout with flexbox on React Native?

To add a 2 column layout with flexbox on React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.

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 style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
      {Array(100)
        .fill()
        .map((_, i) => {
          return (
            <View style={{ width: '50%' }}>
              <Text>{i}</Text>
            </View>
          );
        })}
    </View>
  );
}

to set flexDirection and flexWrap so we get a horizontal flex layout and we wrap overflowing components to the next row.

Then we set each View‘s width to 50% so that they take half the width of the screen.

Conclusion

To add a 2 column layout with flexbox on React Native, we can set flexDirection to 'row' and flexWrap to 'wrap'.