Categories
JavaScript Answers React Native Answers

How to change Text component value dynamically with React Native?

Sometimes, we want to change Text component value dynamically with React Native.

In this article, we’ll look at how to change Text component value dynamically with React Native.

How to change Text component value dynamically with React Native?

To change Text component value dynamically with React Native, we can use a state.

For instance, we write:

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

export default function App() {
  const [myText, setMyText] = React.useState('Original Text');
  return (
    <View>
      <Text onPress={() => setMyText('Changed Text')}>{myText}</Text>
    </View>
  );
}

to define the myText state with useState with initial value set to 'Original Text'.

Then we call setMyText to change the text value when we press the Text component.

As a result, when we press on Text, the text inside changes to 'Changed Text'.

Conclusion

To change Text component value dynamically with React Native, we can use a state.

Categories
JavaScript Answers React Native Answers

How to truncate text component using number of lines with React Native?

Sometimes, we want to truncate text component using number of lines with React Native.

In this article, we’ll look at how to truncate text component using number of lines with React Native.

How to truncate text component using number of lines with React Native?

To truncate text component using number of lines with React Native, we can get it from the onTextLayout callback’s parameter.

For instance, we write:

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

const NUM_OF_LINES = 3;
const SOME_LONG_TEXT_BLOCK =
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque pulvinar sem at leo dapibus maximus. Pellentesque nec scelerisque ipsum. Vestibulum sit amet orci vel augue suscipit maximus. Morbi malesuada augue eu sapien iaculis laoreet. Nunc augue purus, ultricies at auctor non, dapibus a ligula. Integer rutrum congue erat a rutrum. ';

export default function App() {
  return (
    <Text numberOfLines={NUM_OF_LINES} ellipsizeMode="tail">
      {SOME_LONG_TEXT_BLOCK}
    </Text>
  );
}

to set numberOfLines to NUM_OF_LINES to restrict the number of lines to display.

We set ellipsizeMode to 'tail' to add the ellipsis after the truncated text.

Conclusion

To determine number of lines of Text component with React Native, we can get it from the onTextLayout callback’s parameter.

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.