Categories
JavaScript Answers React Native Answers

How to have content break to next line with flex when content reaches edge with Flex React Native?

Sometimes, we want to have content break to next line with flex when content reaches edge with Flex React Native.

In this article, we’ll look at how to have content break to next line with flex when content reaches edge with Flex React Native.

How to have content break to next line with flex when content reaches edge with Flex React Native?

To have content break to next line with flex when content reaches edge with Flex React Native, we can set flexWrap to 'wrap'.

For instance, we write:

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

export default function App() {
  return (
    <View
      style={{
        display: 'flex',
        flexWrap: 'wrap',
        flexDirection: 'row',
      }}>
      {Array(100)
        .fill()
        .map((_, i) => {
          return (
            <Text key={i} style={{ width: 50 }}>
              hello
            </Text>
          );
        })}
    </View>
  );
}

to set flexDirection to 'row' to set the flexbox direction to horizontal.

Then we set flexWrap to 'wrap' to make the child items wrap when it reaches the right edge of the screen.

And finally we add some Text components with width of 50 pixels as children of the View.

As a result, we see the ‘hello’ text displayed in rows.

Conclusion

To have content break to next line with flex when content reaches edge with Flex React Native, we can set flexWrap to 'wrap'.

Categories
JavaScript Answers React Native Answers

How to create a text border in React Native?

Sometimes, we want to create a text border in React Native.

In this article, we’ll look at how to create a text border in React Native.

How to create a text border in React Native?

To create a text border in React Native, we can add a few styles.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flex: 1, padding: 30 }}>
      <Text
        style={{
          borderWidth: 1,
          borderColor: 'thistle',
          borderRadius: 50,
        }}>
        hello
      </Text>
    </View>
  );
}

to add the borderWidth to 1 pixel.

borderColor sets the border color.

borderRadius sets the border corner radius.

Conclusion

To create a text border in React Native, we can add a few styles.

Categories
JavaScript Answers React Native Answers

How to unfocus a TextInput in React Native?

Sometimes, we want to unfocus a TextInput in React Native.

In this article, we’ll look at how to unfocus a TextInput in React Native.

How to unfocus a TextInput in React Native?

To unfocus a TextInput in React Native, we can use the Keyboard.dismiss method.

For instance, we write:

import * as React from 'react';
import { View, Keyboard, TextInput } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View style={{ flex: 1, padding: 30 }}>
      <TextInput onSubmitEditing={Keyboard.dismiss} placeholder="Name" />
    </View>
  );
}

to set the onSubmitEditing prop to Keyboard.dismiss.

As a result, when we press enter when we’re focus on the text input, the focus will move away from the text input.

Conclusion

To unfocus a TextInput in React Native, we can use the Keyboard.dismiss method.

Categories
JavaScript Answers React Native Answers

How to add a borderRadius to ImageBackground with React Native?

Sometimes, we want to add a borderRadius to ImageBackground with React Native.

In this article, we’ll look at how to add a borderRadius to ImageBackground with React Native.

How to add a borderRadius to ImageBackground with React Native?

To add a borderRadius to ImageBackground with React Native, we can set the borderRadius in the imageStyle prop.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <ImageBackground
        imageStyle={{
          borderRadius: 6,
          overflow: 'hidden',
          width: '100%',
          height: 300,
        }}
        resizeMode="cover"
        source={{ uri: 'https://picsum.photos/200/300' }}>
        <Text>Inside</Text>
      </ImageBackground>
    </View>
  );
}

to set the borderRadius to 6 in the object we set as the value of the imageStyle prop of the ImageBackground.

We add a background image with the ImageBackground component.

We add the Text component as the content that goes inside the image background.

Therefore, we see the border radius applied to the background image.

Conclusion

To add a borderRadius to ImageBackground with React Native, we can set the borderRadius in the imageStyle prop.

Categories
JavaScript Answers React Native Answers

How to fix Objects are not valid as a React child error with React Native?

Sometimes, we want to fix Objects are not valid as a React child error with React Native.

In this article, we’ll look at how to fix Objects are not valid as a React child error with React Native.

How to fix Objects are not valid as a React child error with React Native?

To fix Objects are not valid as a React child error with React Native, we should make sure we’re returning strings, null, a component, an array of components, or a fragment.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <Text>hello world</Text>
    </View>
  );
}

to render a View with a Text component inside it.

Therefore, we see ‘hello world’ displayed.

Conclusion

To fix Objects are not valid as a React child error with React Native, we should make sure we’re returning strings, null, a component, an array of components, or a fragment.