Categories
JavaScript Answers React Native Answers

How to import color variables into styles with React Native?

Sometimes, we want to import color variables into styles with React Native.

In this article, we’ll look at how to import color variables into styles with React Native.

How to import color variables into styles with React Native?

To import color variables into styles with React Native, we can use export to export the color variables.

For instance, we write:

colors.js

export const COLORS = {
  white: '#fff',
  green: 'green',
};

to create the color.js module.

Then we write:

App.js

import * as React from 'react';
import { View, TouchableHighlight, Text } from 'react-native';
import { COLORS } from './colors.js';
export default function App() {
  return (
    <View>
      <Text style={{ color: COLORS.green }}>hello</Text>
    </View>
  );
}

to import the COLORS variable with

import { COLORS } from './colors.js';

And we use it to set the color style of the Text component.

As a result, we see that the text is green.

Conclusion

To import color variables into styles with React Native, we can use export to export the color variables.

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