Having trouble getting ImageBackground resizeMode to function within the style props?

I have recently started developing a simple app in react native and I am facing an issue with the resizeMode property of ImageBackground. It seems that the resizeMode is not working when used within the stylesheet, however, it works fine when directly added as a prop to the ImageBackground component.

Below is my code snippet:

/*global require */
...
import {
  StyleSheet,
  ImageBackground,
  View,
  TouchableOpacity,
  Text,
  useWindowDimensions,
    } from "react-native"; 

...

export default function LaunchScreen({ navigation }) {
 ...

  const image = require("../../assets/test.png");
 
  ...
  return (
    <View onLayout={handleLayout} style={styles.container}>
      <ImageBackground source={image} style={styles.image}>
     ...
      </ImageBackground>
    </View>
  );
}

const styles = StyleSheet.create({
...
  image: {
    flex: 1,

     resizeMode: "contain",//--> This is not working.
    justifyContent: "center",
  },


 ...
});

LaunchScreen.propTypes = {
  navigation: PropTypes.object.isRequired,
};

When I add the resizeMode prop directly to the ImageBackground component, it starts working. According to the documentation at https://reactnative.dev/docs/imagebackground, it should work in the stylesheet as well. Any insights on why this behavior is occurring and what could be the solution?

Answer №1

To properly implement resizeMode, refer to the following example:

<ImageBackground
              ...otherProps,
              resizeMode= 'contain'
            >
            {child}
        </ImageBackground>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we prevent other components from re-rendering while using setInterval and updating state within useEffect?

I am working with two components, List.js and Counter.js. In App.js, after the DOM is rendered, I want to use setInterval to display an incremental count every second. Additionally, there is a list that should only update when manually submitted. App.js e ...

Is it a problem with Cucumber Js callbacks or a feature issue?

I would like to create a scenario similar to this: Scenario: initialize new Singleton When an unmatched identity is received for the first time Then create a new tin record And establish a new bronze record And generate a new gold record This s ...

Delivering properties to child components using the react-router version 4

In older versions of react-router (v3.*), I used to pass props to children components like this: React.cloneElement(this.props.children, this.props) Now, in react-router v4 with the new <Match /> API, how can this be achieved? My current solution ...

The appearance of the React user interface in my app does not match what is displayed in the inspect element tool

Strange issue at hand. The progress bar in question appears like this: 1 export default function PercentageBar(props) { 2 return ( 3 <div className="w-full h-1 my-1 bg-stone-200"> 4 <div className={`h-1 bg-orange- ...

Interactive quiz program based on object-oriented principles

As I work on developing a quiz app using JavaScript, everything seems to be going well. However, I've encountered an issue with validation where my code is validating the answers twice - once with the correct answer from the previous question and agai ...

Passing an array with pre-defined variables using jQuery's Ajax functionality

Currently, I have a function set up to gather the contents of a form and send it to a PHP page for processing. However, I am facing an issue where no data is reaching the PHP page when sending it via POST or GET methods. function add_new_customer(){ $ ...

What is the reason for the malfunctioning of this button upon clicking?

Attempting to customize my personal website by making the sidebar width change when the sidebar button is clicked. I'm puzzled as to why it's not working as intended. I prefer to figure it out independently but any helpful tips would be appreciat ...

Leveraging React with axios instead of curl

Can a curl request be made using axios? The curl command is as follows: curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' --data 'client_id=1234&client_secret=1234&grant_type=client_credentials&scope=b ...

Detecting hidden child divs due to overflow: hidden in Angular6

Below is the particular issue I am aiming to address. <div class="row" style="overflow:hidden;"> <app-car *ngFor="let car of cars; trackBy: trackByFunction" [car]="car" > </app-car> </div> <button> ...

Tips to avoid multiple HTTP requests being sent simultaneously

I have a collection of objects that requires triggering asynchronous requests for each object. However, I want to limit the number of simultaneous requests running at once. Additionally, it would be beneficial to have a single point of synchronization afte ...

Issue with loading dynamic content on a webpage using HTML and JavaScript through AJAX

I am currently using the jQuery UI Tabs plugin to dynamically load HTML pages through AJAX. Here is the structure of the HTML code: <div id="tabs"> <ul> <li><a href="pageWithGallery.html" title="pageWithGallery">Gallery< ...

Struggling with a 400 Bad Request Error in Angular with WebAPI Integration

I've been working on creating a database to keep track of comics, and so far I can successfully add new comics and retrieve them using GET requests. However, I've hit a roadblock when trying to update existing comics using PUT requests. Every tim ...

Ajax success handler failing to process JSON response despite receiving status code 200 or 304

My AJAX call is returning a JSON object successfully in the browser. However, instead of firing the success function, the error block is triggered with a message simply stating "error," which doesn't provide much information. The status returned is ei ...

NPM is searching for the package.json file within the user's directory

After completing my test suite, I encountered warnings when adding the test file to the npm scripts in the local package.json. The issue was that the package.json could not be located in the user directory. npm ERR! path C:\Users\chris\pack ...

What is the best way to display a React component or page within a jQuery project?

Looking for advice on integrating React components into an existing jQuery project. I have an old jQuery project where I would like to add a new div to the HTML file and then render my custom React components or pages using ReactDom.render method. If I w ...

Issue with input field not responding when trying to add an item to the cart using React

I'm feeling a bit lost with the logic I am trying to implement here. This is my first time attempting something like this (using an input field to get the number of items to add to cart). Unfortunately, I can't seem to type in the input field. ...

What is the correct method for retrieving a specific value from a JSON object?

I am having trouble using console.log to extract only the name of the item. It's located in the data -> pricing -> tables -> items -> name structure. I want the output to display "Toy Panda". [ { "event": "recipient_completed", ...

JavaScript Fullcalendar script - converting the names of months and days

I recently integrated the Fullcalendar script into my website (https://fullcalendar.io/). Most of the features are functioning correctly, however, I am seeking to translate the English names of months and days of the week. Within the downloaded package, ...

I am looking to modify the ID of the select element nested within a td tag

This is the code snippet I am working with: <tr> <td class="demo"> <label>nemo#2 Gender</label> <select id="custG2" required="required"> <option>....</option> <option>M</option> ...

If the visitor navigated from a different page within the site, then take one course of action; otherwise

How can I achieve the following scenario: There are two pages on a website; Parent page and Inside page. If a user navigates directly to the Inside page by entering the URL or clicking a link from a page other than the Parent page, display "foo". However, ...