What is the best way to display these images correctly within a slider?

My goal is to display 4 images/company logos per slide, but they all end up clustered on one slide. Despite my efforts to adjust the CSS, nothing seems to work.

This is what my code currently renders: https://i.sstatic.net/ThaOK.png

Here are the react component codes I am using:

To see the issue in action, check out this link to the codesandbox: https://codesandbox.io/p/sandbox/admiring-resonance-qptjfh?file=%2Fsrc%2Fcomponents%2FAdCarousel.tsx%3A10%2C1

Explanation of code logic: For each slide, a subset of 4 logos is selected using slice. Then, it maps over this subset to create an img element for each logo. Each img has a key attribute set to the logoIndex for efficient rendering. This method ensures dynamic rendering of logos based on the current slide and their position in the overall array.

Any advice or assistance on how to resolve this issue would be greatly appreciated. Thank you in advance!

Answer №1

My recommendation would be to implement swiper for this specific task. You can find more information here:

Answer №2

Unfortunately, I can't see your code at the moment. However, I can provide you with a helpful example of a React component that utilizes the widely used carousel library called react-slick to achieve the desired functionality. Before proceeding, ensure that you have react-slick and slick-carousel installed via npm or yarn

Installation steps:

npm install react-slick --save

Create a basic React component

import React from 'react';
import Slider from 'react-slick';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';

const LogoSlider = ({ logos }) => {
  const settings = {
    infinite: true,
    slidesToShow: 4,
    slidesToScroll: 1,
    dots: true,
    arrows: true,
  };

  return (
    <Slider {...settings}>
      {logos.map((slideLogos, index) => (
        <div key={index}>
          {slideLogos.map((logo, logoIndex) => (
            <img
              key={logoIndex}
              src={logo.imageUrl}
              alt={`Logo ${logoIndex + 1}`}
              style={{ width: '100%', height: 'auto' }}
            />
          ))}
        </div>
      ))}
    </Slider>
  );
};

export default LogoSlider;

You can now incorporate this component into your parent component:

import React from 'react';
import LogoSlider from './LogoSlider'; 

const YourParentComponent = () => {
  const logos = [
    [
      { imageUrl: 'logo1.jpg' },
      { imageUrl: 'logo2.jpg' },
      { imageUrl: 'logo3.jpg' },
      { imageUrl: 'logo4.jpg' },
    ],
    // ... additional arrays of logos for each slide
  ];

  return (
    <div>
      <h1>Your Logo Slider</h1>
      <LogoSlider logos={logos} />
    </div>
  );
};

export default YourParentComponent;

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

Changes in props do not automatically update children via Ref

I'm working on a React component that needs to update whenever the width changes: import { useRef, useEffect, useState } from "react"; function Child({ containerWidth }) { console.log("CHILD", containerWidth); return <div&g ...

Having issues with InstaFeed (search) feature and jQuery Mobile

As a new developer, I am working on my first JQM site. However, I am facing an issue with my search input form where the instafeed photos do not show up until after a manual page refresh following submission. Despite numerous attempts, I cannot seem to res ...

Can a `react` app with `mysql` be uploaded to `github`?

I recently created a basic online store with the help of react, node, and mysql. I am considering uploading it to github, but I'm uncertain if I can do so while my database is currently stored on localhost. Any advice? ...

Is it possible to develop an image that can be zoomed in and out using the mouse

$(document.createElement('img')) .width(imgW) .height(imgH) .addClass('img_full') .attr('src', $(this).attr('data-src')) .draggable() .css({ &a ...

Simulating a service call in an AngularJS controller

Here is the code for my Controller: (function () { 'use strict'; angular.module('myApp').controller('myCtrl', function ($scope, myService) { // Start -----> Service call: Get Initial Data myService ...

Why is access to fetch from the origin localhost blocked by CORS policy, while posting using ajax does not face this issue?

Let's dive into the difference between AJAX and Fetch without involving CORS. I want to understand why an AJAX POST request works flawlessly while a Fetch POST request fails! Currently, I am using jQuery and AJAX for both GET and POST operations. Whe ...

Struggling to eliminate the underlines from an HTML hyperlink

Can someone help me figure out how to remove the annoying underline link that pops up when hovering over an a element in HTML? I attempted adding a style="text-decoration: none" attribute to the a link, but the underline persists when I hover over the text ...

The process of implementing image validation in PHP, such as checking for size and weight restrictions,

Can someone assist me with inserting an image in PHP, along with validation for size and weight? If the size or weight is incorrect, I need to display an error message. Please provide guidance... PHP script needed... if (isset($_POST['submit']) ...

What is the best way to use res.sendFile() to serve a file from a separate directory in an Express.js web application?

I have a situation within the controllers folder: //controler.js exports.serve_sitemap = (req, res) => { res.sendFile("../../sitemap.xml"); // or // res.send(__dirname + "./sitemap.xml") // But both options are not working }; ...

The horizontal scrollbar in Chrome is unexpectedly activated, despite elements being set to occupy the full screen width (100vw) using Tailwind and NextJS 13.4+

It took some time to identify the root cause of this issue within a larger project. Hopefully, this Question and Answer will be beneficial to someone else. Here is the scenario -- in a NextJS/Tailwind project, there is only one large vertical element on t ...

Deploying a website with a ReactJS user interface and ExpressJS server backend

A React single page application (SPA) serves as the frontend, while a NodeJs application operates as the backend, exposing an API. The frontend interacts with the API periodically but functions mostly independently. When it comes to hosting this type of ...

Tips for restricting the line length in email XSLT

I am currently working on creating a report that needs to be sent via email. However, I have encountered an issue where the lines get cut off if they exceed about 2040 characters in length by email daemons. I have been using XSLT to construct the email rep ...

Capture an image of a webpage and print it out

I am currently in the process of designing a web page and one of the key features I need is a button that allows users to print a selected area. After conducting several searches, I came across html2canvas as a potential solution. I proceeded to install it ...

Encountered a 'SyntaxError: await is only valid in async function' error while trying to utilize the top-level await feature in Node v14.14.0

I'm excited to use the new top-level await feature that was introduced in Node version 14.8. For more information, you can check out this link and here. I did a thorough search but couldn't find any questions related to issues with the new featur ...

Applying unique textures to individual sides in Three.js

Here is the code for my textured cube: const textureLoader: TextureLoader = new TextureLoader(); const textureArray: MeshBasicMaterial[] = [ new MeshBasicMaterial({ map: textureLoader.load("./model/front.jpeg") }), new MeshBasicMaterial({ map ...

Function not executing on button press in React.js

I am trying to trigger a function upon clicking a button, but unfortunately, nothing is happening. Despite adding a console.warn() statement inside the function, it doesn't seem to be logging anything. I've looked through similar Stack Overflow s ...

AngularJS - Viewless and Issue-Free

I'm currently working on a project that involves using AngularJS and PHP. I made some changes, but now it's not functioning properly... The issue is that there are no errors in the console, Angular works (I can retrieve its version), but my vi ...

Keep deciphering in a loop until the URI string matches

I have a task to decode a string URI until there are no more changes. The string URI I am working with typically has around 53,000 characters, so the comparison needs to be fast. For demonstration purposes, I have used a shortened version of the string. B ...

Integrating DHTMLX Scheduler with Node JS for seamless scheduling solutions

Having diligently followed the DTHMLX Scheduler guide, I've encountered an issue with the db.event.insert() function not working, as the associated route fails to trigger. Interestingly, data from my MongoDB displays correctly when inserted via the sh ...

How can I stop the li item from swiping right in JQuery mobile?

I've been implementing this code from https://github.com/ksloan/jquery-mobile-swipe-list and I've made some modifications to it. It's been working well for me so far. However, the code includes two buttons - one on the right and one on the l ...