Categories
JavaScript Answers React Native Answers

How to avoid text wrapping with React Native?

Sometimes, we want to avoid text wrapping with React Native.

In this article, we’ll look at how to avoid text wrapping with React Native.

How to avoid text wrapping with React Native?

To avoid text wrapping with React Native, we can set the numberOfLines prop of the Text component to 1.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flexDirection: 'row' }}>
      <Text numberOfLines={1} style={{ flex: 1, textAlign: 'left' }}>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque
        pulvinar sem at leo dapibus maximus.
      </Text>
    </View>
  );
}

to set numberOfLines to 1 to make the text show in one line and truncate the part that overflows the screen.

Conclusion

To avoid text wrapping with React Native, we can set the numberOfLines prop of the Text component to 1.

Categories
JavaScript Answers React Native Answers

How to use ImageBackground to set background image for screen in React Native?

Sometimes, we want to use ImageBackground to set background image for screen in React Native.

In this article, we’ll look at how to use ImageBackground to set background image for screen in React Native.

How to use ImageBackground to set background image for screen in React Native?

To use ImageBackground to set background image for screen in React Native, we can set the width and height of the background image.

For instance, we write:

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

export default function App() {
  return (
    <ImageBackground
      source={{ uri: 'https://picsum.photos/200/300' }}
      style={{ width: '100%', height: '100%' }}>
      <View>
        <Text>hello</Text>
      </View>
    </ImageBackground>
  );
}

to set the ImageBackground to 100% width and height to make the background image fill the screen.

Conclusion

To use ImageBackground to set background image for screen in React Native, we can set the width and height of the background image.

Categories
JavaScript Answers React Native Answers

How to store value in local storage in React Native?

Sometimes, we want to store value in local storage in React Native.

In this article, we’ll look at how to store value in local storage in React Native.

How to store value in local storage in React Native?

To store value in local storage in React Native, we can use AsyncStorage.

For instance, we write:

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

export default function App() {
  const getEntries = async () => {
    await AsyncStorage.setItem('key', 'val');
    const value = await AsyncStorage.getItem('key');
    console.log(value);
  };

  React.useEffect(() => {
    getEntries();
  }, []);

  return <View></View>;
}

to call AsyncStorage.setItem with the key and value to storage the entry with the key and value.

Then we call AsyncStorage.getItem with the key to return the value with the given key.

Therefore, value is 'val'.

Conclusion

To store value in local storage in React Native, we can use AsyncStorage.

Categories
JavaScript Answers React Native Answers

How to set margin or padding with shorthands in React Native?

Sometimes, we want to set margin or padding with shorthands in React Native.

In this article, we’ll look at how to set margin or padding with shorthands in React Native.

How to set margin or padding with shorthands in React Native?

To set margin or padding with shorthands in React Native, we can use the marginVertical, marginHorizontal, paddinHorizontal and paddingVertical properties.

For instance, we write:

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

export default function App() {
  return (
    <View
      style={{
        marginVertical: 10,
        marginHorizontal: 20,
        paddingHorizontal: 20,
        paddingVertical: 20,
      }}>
      <Text>hello</Text>
    </View>
  );
}

to set the margin and padding with the properties.

marginVertical set the vertical margins.

marginHorizontal set the horizontal margins.

paddingHorizontal set the horizontal paddings.

paddingVertical set the vertical paddings.

Conclusion

To set margin or padding with shorthands in React Native, we can use the marginVertical, marginHorizontal, paddinHorizontal and paddingVertical properties.

Categories
JavaScript Answers React Native Answers

How to update array state in React Native?

Sometimes, we want to update array state in React Native.

In this article, we’ll look at how to update array state in React Native.

How to update array state in React Native?

To update array state in React Native, we can call the state setter function with a function that returns the new array we want.

For instance, we write:

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

export default function App() {
  const [arr, setArr] = React.useState(['foo', 'bar', 'baz']);

  return (
    <View>
      {arr.map((a, i) => {
        return (
          <TextInput
            value={a}
            key={i}
            onChangeText={(t) => {
              setArr((arr) => {
                const newArr = [...arr];
                newArr[i] = t;
                return newArr;
              });
            }}
          />
        );
      })}
      <Text>{arr.join(', ')}</Text>
    </View>
  );
}

to define the arr state with useState.

Then we call arr.map with a callback that renders a TextInput.

Next, we set onChangeText to a function that calls setArr with a callback that updates the arr array.

In the function, we make a copy of arr and assign it to newArr.

Then we assign newArr[i] to the input value t.

And then we return newArr.

As a result, when we type something into the text input, we see the Text display change to the typed value.

Conclusion

To update array state in React Native, we can call the state setter function with a function that returns the new array we want.