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.

Categories
JavaScript Answers React Native Answers

How to create a UI with box shadow in React Native?

Sometimes, we want to create a UI with box shadow in React Native.

In this article, we’ll look at how to create a UI with box shadow in React Native.

How to create a UI with box shadow in React Native?

To create a UI with box shadow in React Native, we can set some shadow 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>
      <View
        style={{
          shadowColor: '#000',
          shadowOffset: { width: 0, height: 1 },
          shadowOpacity: 0.8,
          shadowRadius: 1,
        }}>
        <Text>hello world</Text>
      </View>
    </View>
  );
}

to add a view with a box shadow around it.

shadowOffset adds the shadow effect.

shadowColor sets the shadow color.

And shadowOpacity sets the shadow opacity.

Conclusion

To create a UI with box shadow in React Native, we can set some shadow styles.

Categories
JavaScript Answers React Native Answers

How to fix the React Native FlatList last item not visible issue?

Sometimes, we want to fix the React Native FlatList last item not visible issue.

In this article, we’ll look at how to fix the React Native FlatList last item not visible issue.

How to fix the React Native FlatList last item not visible issue?

To fix the React Native FlatList last item not visible issue, we can add bottom padding to the FlatList.

For instance, we write:

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

const DATA = Array(100)
  .fill()
  .map((_, i) => ({ id: i, title: i }));

const Item = ({ title }) => (
  <View>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View>
      <FlatList
        style={{ height: '100%', paddingBottom: 50 }}
        data={DATA}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to add a FlatList and set its paddingBottom style to 50 pixels to add some padding at the bottom of the FlatList.

We render the item with the renderitem function to render each DATA entry.

And we set keyExtractor to a function to return the unique ID property value from the DATA entries.

Conclusion

To fix the React Native FlatList last item not visible issue, we can add bottom padding to the FlatList.

Categories
JavaScript Answers React Native Answers

How to add a rounded image with a border with React Native?

Sometimes, we want to add a rounded image with a border with React Native.

In this article, we’ll look at how to add a rounded image with a border with React Native.

How to add a rounded image with a border with React Native?

To add a rounded image with a border with React Native, we can set the borderRadius and overflow styles.

For instance, we write:

import * as React from 'react';
import { View, Image } 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>
      <Image
        source={{ uri: 'https://picsum.photos/200/300' }}
        style={{
          width: 150,
          height: 150,
          borderRadius: '50%',
          overflow: 'hidden',
          borderWidth: 3,
          borderColor: 'red',
        }}
      />
    </View>
  );
}

to set borderRadius to '50%' to make the Image round.

And we set the width and height of the Image to set the dimensions.

We set overflow to 'hidden' so the Image stays in the circle.

Conclusion

To add a rounded image with a border with React Native, we can set the borderRadius and overflow styles.