What is the best way to configure my card components for a flex display?

Currently, I am in the process of learning React. I have a file named new_card_component.js that is responsible for displaying data fetched from the data.js file. The issue arises in my AirBnB.js file, where I'm passing the AirbnbApp module to a section called data-container, which has a flex property. However, the card components are not being displayed in a flex layout as expected.

import "../CSS/airbnb.css";
import AirbnbApp from "./airbnbApp";

function AirBnBPage(){
    return (
        <div>
            <NavBar/>
            <Hero/>
            <section className="data-container">
                <AirbnbApp/>
            </section>
        </div>
    )
}
export default AirBnBPage

In my new_card_component.js file:

import { FaStar } from "react-icons/fa6";
import "../CSS/airbnb.css";

function NewCard(prop){
    return (
        <div className="card-sub">
            <img src={prop.mainimage} alt="trainer"/>
            <div className="info-group">
                <a href="https://"><FaStar/> {prop.rating} ({prop.review})</a>
                <p>{prop.country}</p>
            </div>  
            <p>{prop.message}</p>
            <div className="info-group">
                <p>Available spots : {prop.spots}   Price : {prop.cost} $</p>
            </div>
        </div>
    )
}

My CSS file - airbnb.css:

.data-container{
    /* background-color: red; */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width:1200px;
    margin:0 auto;
    align-items: center;
    justify-content:flex-start;
}
.card-sub{
    margin:25px 0 0;
    width:320px;
    border:2px solid black;
}
.card-sub img{  
    width:220px;
    height: 300px;
    border-radius: 10px;
}
.info-group{
    margin-top:10px;
    display: flex;
    gap:0.7rem;
    margin-left:10px;
}

I attempted to apply the display:flex property to the data-container section, but it did not resolve the issue. The card components are still being displayed on separate rows despite having an array with 4 objects in my data.js file, resulting in each card component being shown on its own row.

Answer №1

It turned out that the issue was actually in the file removed from the question, lucky I checked before proceeding.

In the AirbnbApp.js, make sure to return the cardElements directly instead of wrapping them inside a div. Using display: flex affects direct children, so avoid unnecessary div wrappers.

       }].map(function (cardItem) {
            return <NewCard mainimage={cardItem.coverImage} rating={cardItem.stats.rating} country={cardItem.location} message={cardItem.description} cost={cardItem.price} spots={cardItem.openSpots} review={cardItem.stats.reviewCount} />
        })

    return cardElements;
}

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

Challenges of Using Flexbox in Media Queries

How can I use flex boxes in CSS to ensure that from a min-width of 769px to 1025px, the third figure is displayed on a new line while the first and second figures remain on the top line taking up equal space? <div class="mid-col-section-2"> <figu ...

How can I resolve the error 'unable to pass refs to function components' when attempting to choose menu items in a separate component?

Currently, I am facing an issue while attempting to relocate my menu items from a select field to a separate component. As I need to utilize the same menu items twice, I thought it would be best to move them to a separate component. However, this approach ...

Leveraging a returned value from a function in Typescript within a React component

I am currently facing a challenge where I need to extract values from a Typescript function and use them to dynamically render a React page. Specifically, the two values I am working with are the outputs of getIcon() and getSpaces(). This is how I attempt ...

What is the process for incorporating Material-UI into a JSFiddle project?

I'm having trouble figuring out how to load and use the Material UI script on platforms like JSFiddle or SO's code editor, even though I can add it with NPM. Check out this JSFiddle example <script src="https://unpkg.com/@material-ui/core/um ...

Check the output of the ChildProcess after executing a shell command

I am currently running the ChildProcess function within a Nextjs API route and I am struggling to retrieve the value from it. const output = exec( "curl -s -v https://test.com/index.php", (err, stdout, stderr) => { if (err) { ...

What is the reason why the show() function in JQuery only applies to one specific element, rather than all elements selected with the same selector?

JSFiddle. This example code features a nested <table> within another <table>. The main issue concerns the click listener for the #add button. Specifically, the final if/else statement in the function. When you execute this code and click the ...

The function 'appendChild' is not recognized on the type 'unknown'.ts(2339)

I'm encountering an issue while trying to integrate the Utterances component into my articles. Upon attempting to build the site, I receive the following error message: "Property 'appendChild' does not exist on type 'unknown' ...

I'm struggling to achieve the placement of my logo and navigation bar side by side

Check out the codes here body{ margin: 0; } .logo { text-indent: -999999px; background: url('logo3.png'); width: 216px; height: 219px; } .header{ width: 100%; height: auto; background-color: #eef3f5; padd ...

What is the best way to obtain the output produced by a function when a button is clicked

When I click on a button, the desired action is to trigger a function that adds a new property inside an object within a large array of multiple objects. This function then eventually returns a new array. How can I access and utilize this new array? I am ...

React js: Changing Material-UI functional code to class component results in TypeError

After utilizing the material ui login page code, I discovered that it is a functional component. To meet my specific requirements, I decided to convert it into a class component. However, during the conversion process, an error was encountered: "Cannot ass ...

What is the most effective way to toggle the visibility of these rows within a table?

Initially, my plan was to incorporate collapsible rows into my table. However, considering the spacing and margins, I realized that approach wouldn't work. After some thought, I concluded that all I really need is a way to hide and show information on ...

The necessity of using Adobe Photoshop for optimizing a Web Template

My interest lies in utilizing web templates, such as the one referenced here:- templete However, within its details tab, it specifies that the following software is required:- Adobe Photoshop CS+ Sublime Text2 or later, Notepad++, Dreamweaver CS5.5+(Co ...

Incorporating a TypeScript module into a JavaScript module within a React application

I'm encountering an issue with my React app that was created using create-react-app. I recently added a Typescript module to the project, which is necessary for functionality reasons. Although it will remain in Typescript, I made sure to install all t ...

Detection of TextField blur event in Material UI

Is there a way to detect when the user leaves or focuses away from a field in Material UI TextField? I am looking for two events - one for entering and one for leaving the field. I know that we can use onFocus to handle entering the field, but is there an ...

Reactjs-ffsevents does not exist as a function

An error occurred: TypeError: fsevents is not a function. This issue is located in the FSEventsWatcher file at line 162. A new FSEventsWatcher was attempted to be created in jest-haste-map, triggering this error. The watcher creation process involved map ...

What could be causing my React child component to not update when changes are made to an array passed down in props after fetching new data?

My Profile.js component is responsible for fetching activity data related to a specific user from the URL parameter and updating the profileActivity state. This state is then passed down to my child component, ProfileActivity.js, where it should be display ...

Discover the countdown to an upcoming event with MomentJS

I am in the process of calculating the remaining time until an event begins. For instance, if my event is set for tomorrow at 7:30 pm, I aim to display the number of hours and minutes left before the event commences. My current approach involves checking ...

Is this an effective and appropriate method for establishing media queries?

<link id ="style" rel="stylesheet" type="text/css" title="other" href="regularStyles.css" /> <link rel="stylesheet" media= "all and (mid-width: 767px) and (max-width:2049px) and (min-height:767px) and (max-height: 1538px)" href="notePads.css" /& ...

Displaying the information from a nested array of objects in an HTML table through iteration

In the code snippet below, there is an input with a nested array of objects. The main array of objects is called summary and within it, there's a nested array called run_type. let input = { "summary": [ { " ...

static with express-rate-limit

I am encountering an issue with my VPS setup that includes Express and React bundle. The problem arises when I try to access the API from the frontend, as both are being assigned the same IP address (localhost). This is causing issues with using express-ra ...