Categories
JavaScript Answers React Native Answers

How to add a button with icons in React Native?

Sometimes, we want to add a button with icons in React Native.

In this article, we’ll look at how to add a button with icons in React Native.

How to add a button with icons in React Native?

To add a button with icons in React Native, we can use the react-native-elements package to add the icon.

To install it, we run npm i react-native-elements.

Then we write:

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

export default function App() {
  return (
    <View>
      <TouchableHighlight onPress={() => {}}>
        <View style={{ flexDirection: 'row' }}>
          <Icon name="rowing" />
          <Text>row</Text>
        </View>
      </TouchableHighlight>
    </View>
  );
}

to add a TouchableHighlight to provide the highlight effect for the button.

Then we add the button content in the View.

We make the Icon and Text display side by side with flexDirection set to 'row'.

Then we add the Icon and Text inside the View.

Conclusion

To add a button with icons in React Native, we can use the react-native-elements package to add the icon.

To install it, we run npm i react-native-elements.

Categories
JavaScript Answers React Native Answers

How to change the text color of text input in React Native?

Sometimes, we want to change the text color of text input in React Native.

In this article, we’ll look at how to change the text color of text input in React Native.

How to change the text color of text input in React Native?

To change the text color of text input in React Native, we can set the color style to the text color.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <TextInput style={{ color: 'green' }} />
    </View>
  );
}

to set the color style to 'green' on the TextInput.

Therefore, we see that the color of the text we typed into the TextInput is green.

Conclusion

To change the text color of text input in React Native, we can set the color style to the text color.

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.