The configuration of flexDirection seems to be making images vanish

I have successfully arranged my images to display one after another, but I want them to be positioned horizontally instead of vertically. I attempted to achieve this by using flexDirection: 'row' and setting scrollview to horizontal, however, the images seem to disappear altogether when applying flexDirection.

Initially, I placed flexDirection in the View at line 60. I also tried wrapping another parent view around it, but encountered the same issue.

If you copy and paste this code into snack.expo.io, you can see exactly what I mean!

Any insights or suggestions are greatly appreciated!

import React, { useState } from 'react';
import {
  Pressable,
  StyleSheet,
  Text,
  View,
  useWindowDimensions,
  Dimensions,
  Image,
  Animated,
  PanResponder,
  TouchableOpacity,
  ScrollView,
  ImageBackground,
  Platform,
} from 'react-native';
import { SearchBar } from 'react-native-elements';

import {
  scale,
  verticalScale,
  moderateScale,
  ScaledSheet,
} from 'react-native-size-matters';

import { MaterialCommunityIcons } from '@expo/vector-icons';

const screenResolutions = () => {
  var SCREEN_HEIGHT = useWindowDimensions().width;
  var SCREEN_WIDTH = useWindowDimensions().height;
};

const images = [
  { id: '1', uri: require('./assets/snack-icon.png'), text: 'Test' },
  { id: '2', uri: require('./assets/snack-icon.png') /*text: "Test"*/ },
  { id: '3', uri: require('./assets/snack-icon.png') /*text: "Test"*/ },
  { id: '4', uri: require('./assets/snack-icon.png') /*text: "Test"*/ },
];

const pressableTest = () => {
  let textlog = '';
  const [state, setState] = useState(0);
};

export default class Home extends React.Component {
  renderImages = () => {
    return images.map((item, i) => {
      return (
        <View
          style={{
            paddingLeft: scale(10),
            paddingRight: scale(10),
            paddingBottom: scale(15),
          }}>
          <TouchableOpacity
            onPress={() => this.props.navigation.navigate('VenueDetails')}>
            <ImageBackground
              source={item.uri}
              style={{
                width: '100%',
                height: scale(225),
                shadowColor: '#000',
                shadowOffset: { width: 1, height: 4 },
                shadowOpacity: 1,
              }}
              imageStyle={{ borderRadius: 10 }}>
              <View
                style={{
                  position: 'absolute',
                  bottom: 10,
                  left: 10,
                  justifyContent: 'flex-start',
                  alignItems: 'flex-start',
                }}>
                <Text style={styles.name}>Name</Text>
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                  <Text style={styles.category}>Category</Text>
                  <Text style={styles.dot}>⬤</Text>
                  <Text style={styles.money}>$$</Text>
                  <Text style={styles.dot}>⬤</Text>
                  <Text style={styles.starRating}>★★★</Text>
                </View>
              </View>
            </ImageBackground>
          </TouchableOpacity>
        </View>
      );
    });
  };

  state = {
    search: '',
  };

  updateSearch = (search) => {
    this.setState({ search });
  };

  render() {
    const { search } = this.state;

    return (
      <ScrollView
        style={{ flex: 1, backgroundColor: '#272933', horizontal: 'true' }}>
        <View style={{ flex: 1, marginTop: 15 }}>{this.renderImages()}</View>
      </ScrollView>
    );
  }
}

const styles = ScaledSheet.create({
  starRating: {
    color: 'white',
    fontSize: '20@s',
    textShadowOffset: { width: 2, height: 2 },
    textShadowRadius: 2,
    textShadowColor: '#000',
  },

  category: {
    color: 'white',
    fontSize: '20@s',
    textShadowOffset: { width: 2, height: 2 },
    textShadowRadius: 2,
    textShadowColor: '#000',
  },
  name: {
    fontWeight: 'bold',
    color: 'white',
    fontSize: '25@s',
    textShadowOffset: { width: 2, height: 2 },
    textShadowRadius: 2,
    textShadowColor: '#000',
  },
  dot: {
    color: 'white',
    fontSize: '5@s',
    paddingLeft: '5@s',
    paddingRight: '5@s',
    textShadowOffset: { width: 2, height: 2 },
    textShadowRadius: 2,
    textShadowColor: '#000',
  },
  money: {
    color: 'white',
    fontSize: '20@s',
  },

});

Answer №1

I may not be familiar with React, but I recommend ensuring that the image view you return from your render images function has a specified width or flex-basis property. Additionally, your container (consisting of a scrollview nested inside a view) should have a CSS style of display: flex and a width of 100vw.

If you're interested, I was able to successfully implement this solution in my project. You can check it out here.


.flex-box {
    width: 100vw; // can also use percentage (%)
    display: flex;
    overflow-x: scroll;
    flex-wrap: no-wrap;
}
.flex-item {
    width: 200px; // can adjust using percentage, flex-basis without shrinking, or min-width
}

Update: I recently modified the code to utilize the "no-wrap" property along with fixed widths instead of basis for horizontal scrolling functionality. By setting a container to display as 100% flex with no wrapping, and assigning fixed widths to its children, you can achieve horizontal overflow. To make sure all content is visible and scrollable, simply include the CSS rule overflow-x: scroll;

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

Creating dynamic queries in Node.js and MongoDB by combining both constants and variables

I am faced with a situation where I need to create a field name dynamically using a variable and some constants. While I know how to use just the variable to form the field name. var index = parseInt(req.body.index); db.collection("user_data").update( { ...

Position the button to the left of the input field

I need to create a button with the id #searchBtn positioned to the left of an input form with the id #searchInput. HTML: header { position: relative; z-index: 2; } .searchInputMain { width: 300px; height: 40px; } .searchButton { width: 200px ...

Modifying the color scheme of Bootstrap

In order to simplify referencing, I am establishing new theme colors as shown below, allowing me to use classes like bg-red instead of bg-danger or text-purple, and so forth. $primary : #24305e; $blue : #375abb; $indigo : #796eff; ...

Displaying and hiding a div element using Vue.js dynamically

I have a scenario with two different sized divs: <div class = "bigger"> </div> <div class = "smaller"> </div> The goal is to hide the bigger div and show the smaller div when the screen width is ...

Combining React with the Reactstrap library, CSS Modules, and Webpack

Struggling to integrate ReactJS, ReactStrap, and CSS-Modules like react-css-modules or bootstrap-css-modules? The challenge lies in combining these modules seamlessly to achieve the desired effect. Unfortunately, online resources have not been much help. ...

What is the process to insert a record into a table by triggering an AJAX call upon clicking "save

I'm looking to dynamically update a table with data from a database using AJAX. Specifically, I want the table to reflect any new records added by the user without having to refresh the entire page. Below is my JavaScript code snippet for handling thi ...

What are the steps for creating a dropdown menu that allows for easy selection of the correct item

I am dealing with a dropdown menu as shown below. <select name="slt"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="3">Three +</option&g ...

Access the child component within an @ChildComponent directive in Angular

Is it possible to retrieve child components of another component? For instance, consider the following QueryList: @ContentChildren(SysColumn) syscolumns: QueryList<SysColumn>; This QueryList will contain all instances of the SysColumns class, which ...

Encountering issues with loading styles in Vue single file components

While working on my project at this link, I encountered an issue with displaying a styled modal. Despite trying to import the styles using: <style scoped> @import "../styles/modal-style.css"; </style> and even directly pasting the co ...

Encountering an undefined json array when making an AJAX request

There have been numerous questions on this topic, but none of the specific solutions seemed to apply to my situation. So, I apologize if this is a duplicate query. I am currently working on fetching data from an SQL database using a PHP file that passes t ...

The initial click on a jQuery event is not registering as expected

My webpage includes a code snippet like this: $('#language_id').change(function () { var urli = 'https://example.com/php/get_arch.php?language_id=' + language_id; $.ajax({ type: "GET", url: urli, dataType: &ap ...

What could be the reason why the Google Maps API fails to load on Firefox?

I am currently utilizing the v3 version of Google Maps API. While my map functions perfectly in Safari and Chrome, I encountered an issue in Firefox where I received the error message "google.maps.Map is not a constructor." When inspecting 'google.ma ...

How can we stop the parent modal from closing if the child component is not valid?

I have a main component with a modal component that takes another component as a parameter. The child modal component has some logic where I need to check if the child component is valid before closing the modal. const MainComponent: FC<IProps> => ...

"Utilizing the flex box feature in IE 10 for efficient text trunc

.container { background: tomato; width: 100px; display: flex; } .text-left { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .text-right { } <div class=container> <div class="text-left"> title title title ...

"Triggering an AJAX POST request with a click event, unexpectedly receiving a GET response for

Hello there! I am currently attempting to send an AJAX POST request when a button is clicked. Below is the form and button that I am referring to: <form class="form-horizontal" > <fieldset> <!-- Form Name --> <legend> ...

Tips for transferring HTML code to a controller

Currently facing an issue while working with MVC and attempting to store HTML code from a view in a database field. In the JS section of my MVC solution, I have the following code snippet: var data = { id_perizia: $("#id_perizia").val(), pinSessione: $("# ...

Associate the ng-model with ng-options value and label

I'm currently using ng-options to create a select tag that displays location options. The labels represent the location names, while the values indicate the corresponding location ID as stored in the database. In addition to binding the value (locati ...

Tips for integrating elements within React Components to create a Container-like structure

I am looking to create a component similar to the following: class MyContainer extends React.Component { render(){ return <div width="1000">{xxx }</div> } } and it should be used in this way: <MyContainer><xxx>&l ...

Looking to automatically scroll to the bottom by clicking on text using javascript/jquery?

I am currently working on a website project where I have a specific requirement. I need the webpage to scroll towards the bottom when a particular text is clicked, using JavaScript/jQuery. <p class="ml-2 mb-2 d-flex view_profile_options"><a hre ...

Guide to creating a Stencil-powered React library using NX, ready for publishing on npm

I am working with a NX monorepo that has been set up with Stencil. Within this setup, I have created Stencil web components and generated a React library using Stencil. Using the React library in a React app within the same repository works just fine. Now ...