ImageBackground - React Native
Overview
When designing a card component or banner with some text inside the image, it's fairly usual to want to use an image as the background. If you're unfamiliar with React Native, you might be asking how to get the same result. Fortunately, there is an included component called ImageBackground that makes this quite simple to use. I will show you how to utilize it in this article.
What is ImageBackground in React Native?
With Expo and React Native projects, you can use the ImageBackground React Native component to display an image as the background of another component. For instance, you can use React Native ImageBackground inside the screen's container view to set the background picture of a screen in your app.
The backgroundImage DOM style property and the background-image stylesheet property of CSS are semantically related to this element. The React Native ImageBackground tag requires a source, that is, the link to the image file you want to use as background.
How to Use React Native ImageBackground?
The ImageBackground component accepts, with a few exceptions, essentially the same props as the Image component. A view that wraps an inner image is given the style prop, and the picture itself is given the imageStyle prop. Moreover, the inner image is affected by the imageRef prop.
ImageBackground Props
The React Native ImageBackground component accepts all the props of the Image component. Other than that, it accepts the following props :
Image Props : Inherits Image Props.
- accessible: It is of Boolean type and the default value is false. When true, the picture serves as an accessibility element.
- accessibilityLabel: It is of string type.The text that the screen reader reads when the user clicks on a picture.
- alt: When a user interacts with a picture, the screen reader will read an alternate text description of the image that is defined by a string. By using this, this element will be marked as accessible by default.
- blurRadius: It is of inqteger type. The blur radius of the image's blur filter.
- capInsets iOS : The image's borders and center content will expand when it is enlarged, but the corners of the size provided by capInsets will remain fixed. For making rounded buttons, shadows, and other resizable components, this is helpful. More details may be found in the Apple documentation.
- crossOrigin: A keyword string indicating the CORS fetching mode to employ for the image resource. It functions similarly to HTML's cross-origin property.
- defaultSource : A still picture to show while the image source loads.
- fadeDuration : It is specific to android. Duration of the fade animation in milliseconds.
- height : Height of the image component.
- loadingIndicatorSource : This attribute, is like source, denotes the source utilised to produce the image's loading indication. The loading indication is shown until the picture is prepared for display, which usually happens after the image has been downloaded.
- onError : Invoked on load error.
- onLayout : Invoked on layout changes or on the mount.
- onload : Triggered after a load is completed.
- onLoadEnd : Invoked after a successful or unsuccessful load.
- onLoadStart : Invoked on load start.
- width : Width of the image component.
- tintColor : All non-transparent pixels' colours are adjusted to the tintColor.
- src : a string identifying the image's remote URL. The source prop is subordinate to this prop.
- source : The image's source, which might be a local file resource or a remote URL. This prop may additionally have several remote URLs that are supplied along with their width, height, and sometimes scale or other URI parameters. Afterward, based on the estimated size of the image container, the native side will select the ideal uri to show.
- backfaceVisibility : Whether or not a rotated image's rear face should be seen is determined by the attribute.
- overlayColor: Choosing an overlayColor will result in the image's rounded edges' leftover space being filled with a solid color. This is helpful in situations when the Android implementation of rounded corners is not supported.
- resizeMode : When the frame doesn't match the raw image dimensions, the resizeMode setting decides how to resize the picture. It has a default value of cover.
- objectFit: When the frame doesn't match the raw image dimensions, objectFit decides how to resize the picture.
- Other common props are backgroundColor, borderBottomLeftRadius, borderBottomRightRadius, borderColor, borderRadius, borderTopLeftRadius, borderTopRightRadius, and borderWidth.
Examples of React Native ImageBackground
To understand the use of React Native ImageBackground component, now let us set up a new React Native project by the following commands :
Now to run the project, use the below command :
Now in the App.js file, write the following code :
FAQs
Q. What are the various ways the background image can be resized according to the container?
A. There are various values for image’s resize mode that can be used :
- cover: Maintain the aspect ratio of the image by evenly scaling it so that both or at least one of its dimensions (width and height) is equal to or bigger than the corresponding dimension of the view (minus padding)
- contain: To ensure that the image's width and height are equal to or less than the corresponding dimension of the view, scale the image uniformly while maintaining its aspect ratio (minus padding).
- stretch: Scale width and height separately; this may alter the src's aspect ratio.
- repeat: To fill the view's frame, repeat the image. Unless it is larger than the view, in which case it will be evenly scaled down to fit inside the view, the image will maintain its size and aspect ratio.
- center: The image should be in the view's center along both dimensions. Scale the image evenly down if it is bigger than the view to fit inside the frame.
Q. What is the difference between imageStyle and style prop?
A. The imageStyle is used to provide styling to the Image component of the React Native ImageBackground, while the style prop is used to modify the View component of React Native ImageBackground.
Conclusion
- ImageBackground can be used inside the screen's container view to set the background picture of a screen in your app.
- The backgroundImage DOM style property and the background-image stylesheet property of CSS are semantically related to this React Native ImageBackground.
- With Expo and React Native projects, you can use the React Native ImageBackground component to display an image as the background of another component.
- The React Native ImageBackground component accepts all the props of the Image component in addition to imageRef, imageStyle, and Style.