Tips for nesting React components within a grid layout

For my News website, I have a component that showcases articles fetched from an API in a grid format. Additionally, I also have a separate component dedicated to displaying the Latest News. I am looking for a way to position the Latest News widget within the article grid so that it appears as the 3rd and 6th elements. Here is an example of what I am aiming for: https://i.sstatic.net/wzsLc.jpg

Any ideas on how to achieve this?

Answer №1

Here is an example of how your return statement can be structured. Simply remove the first four elements and map the rest. In this specific case, I have used the MUI grid component, but the underlying logic remains consistent across different implementations.

return (
    <>
      <Grid container spacing={2}>
        <Grid item lg={8}>
          <Grid container spacing={2}>
            <Grid item lg={6}>
              {news[0]}
            </Grid>
            <Grid item lg={6}>
              {news[1]}
            </Grid>
            <Grid item lg={6}>
              {news[2]}
            </Grid>
            <Grid item lg={6}>
              {news[3]}
            </Grid>
          </Grid>
        </Grid>
        <Grid item lg={4}>
          <Grid container>
            <Grid item> Latest News</Grid>
          </Grid>
        </Grid>
        {news.slice(4).map((new) => {
          return <Grid item lg={4}>{new}</Grid>;
        })}
      </Grid>
    </>
  );

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

What is the best way to save the data received from createApi into the Redux store?

Currently, I am faced with the challenge of storing user data (such as name, email, etc.) obtained through the createApi function into Redux store. However, I'm unsure of the best practice to achieve this. In my userApi.js file: export const userApi ...

Navigating the HTMLcollection in React.js

Struggling to properly iterate through an HTMLCollection within a React.js component. Trying to make use of the following function: shuffleLists = () => { var elems = document.getElementsByTagName("ul"); console.log(elems) for ...

The access token obtained through MSAL lacks group identification

How can I include group IDs in the access token retrieved using MSAL in my React web client? Here is how I currently retrieve the access token: const accessToken = await instance.acquireTokenSilent({ scopes: [ "https://graph.windows.net/Grou ...

Displaying a collection of items using ReactJS

Hey there! I have an array of objects containing comments for a particular item, and I only want to display the first 10 on the page. Below that list, I'm looking to add a button that allows users to see the next set of 10 comments when clicked. It&a ...

When hovering over items in the navigation bar, the entire bar expands seamlessly

I've spent countless hours trying to troubleshoot an issue with my dropdown menu that expands whenever I hover over it. The bar stretches out to accommodate the list of dropdown contents and then reverts back to normal when I move my cursor away. My a ...

What about CSS block elements that have a width relative to their parent container

My goal is to create a layout where each h3 and p element is wrapped in a box. The current issue I'm facing is that the blocks extend to full width due to h3 and p being block level elements. The desired outcome is for the width of the boxes to adjus ...

"MongoDB-powered React application runs smoothly on local environment but encounters issues on Heroku

I am currently facing an issue while trying to deploy a react site on Heroku. Everything seems to be working fine except for the fact that the data from my MongoDB Atlas is not being sent to Heroku. When I run the app locally, it works perfectly as expecte ...

CSS hover effects are not displayed when using the input type button

I'm having issues with my hover effects not showing: <input type="button" class="button" value="Click"> However, this code works fine: <button class="button">Click</button> The CSS codes are ...

Is the footer html/css duplicated on a different page and displaying differently?

I have been working on a website and just finished the main page with the header, body, and footer. Now, as I moved on to creating the second page, I encountered an issue. I copied and pasted the code for the header from the main page onto the second page ...

JavaScript function to toggle visibility and scroll back to top of the page

I have been an avid reader on this platform, and I am hoping that my question has not already been addressed. I am struggling to find a solution to a problem I am facing. There are two DIVs in my code that I toggle between showing and hiding using Javascr ...

Developing a Next.js shared library

I am looking to create Next.js components as shared components that can be utilized across multiple projects. 1- To get started, I have developed a basic component named "TextBox" located at src/lib/component/TextBox.js: import { useState } from "react"; ...

What is the best way to ensure my checkboxes are positioned on individual lines?

I am trying to vertically align my checkboxes so that each option appears on a separate line. I searched online and found suggestions to use vertical-align: middle, but that only changes the position of the label. I want each checkbox to be displayed on it ...

Capturing a part of the screen using JavaScript for screen recording

I'm exploring the possibility of implementing a JavaScript screen recorder that captures a video feed rather than the entire screen. I'm wondering if it's achievable using getDisplayMedia or if there's a specific library for this purpos ...

"Implementing a seamless transition effect on two slideshows using jQuery's fadeslideshow

I currently have a homepage featuring a fadeslideshow with 5 slides. The fadeslideshow plugin being used can be found at http://plugins.jquery.com/project/fadeslideshow. On the homepage, there is also a menu bar with various menu items. When a user clicks ...

Align text in the middle of an image

I need some help with formatting my web page. I want to move the content of the red rectangle into the green rectangle, like in the image below: https://i.stack.imgur.com/sAved.jpg The goal is to center the text with the picture. I've tried using di ...

Customizing the input placeholder for password fields in IE8 using jQuery

Currently, I have opted to use jQuery instead of the HTML5 placeholder attribute <input type="text" name="email" value="Email" onfocus="if (this.value == 'Email') { this.value = ''; }" onblur="if (this.value == '') { this. ...

How can I define Record values in Typescript based on their specific keys?

I am working on creating a custom data structure that allows me to store values with string keys within the union string | number | boolean: type FilterKey = string; type FilterValue = string | number | boolean; type Filters<K extends FilterKey, T exten ...

The navbar toggler icon is not displaying

<header class="container"> <div class="container pt-3 col-md-5 col-sm-5 col-xs-6 text-center"> <a href="#" class="site-logo"><img src="#"></a> <button class="navbar-toggler" type="button" data-toggle="collapse" ...

Encountered a problem locating the "./aws-exports" file in the "App.js" directory

For a while now, I have been working on developing an application using awsamplify. Today, I decided to conduct some tests and when I executed npm run start-web Everything ran smoothly. Next, I proceeded to test the mobile version using Expo by running ...

``I am experiencing difficulties with utilizing a personalized color scheme in React JS with Material

I'm currently working on customizing the color palette for my project, but I am only able to modify the main attribute and not others. My development environment is JavaScript with MUI, not Typescript. import './App.css'; import {BrowserRout ...