React and Ant Design Carousel

Viewed 1060

I'm trying to use antd Carousel in my react app. I’ve gone through numerous resources for ideas on how to use ant design carousel with images but I've not found any example describing not to use 'style' and other things when using antd Carousel.

I want to use <Carousel><Carousel/> in <Content><Content/> as first in my App.js file and want to do CSS separately in App.css file for Carousel.

Does anyone have any tips in general for how can I get the antd Carousel in my one page site to display some catchy images?

my code:

import React from 'react';
import "../../src/App.css";
import 'bootstrap/dist/css/bootstrap.min.css';
import { TwitterOutlined, FacebookOutlined, YoutubeOutlined, PauseOutlined, SearchOutlined, LeftSquareOutlined, RightSquareOutlined } from '@ant-design/icons';
import { Navbar, Nav } from 'react-bootstrap';
import { Layout, Carousel, } from 'antd';

}
const { Header, Footer, Content } = Layout;
class App extends React.Component {
render() {
    return (
      <Layout>
        <Header>
        <Navbar expand="lg">
            <Navbar.Brand href="#home">
                <img src={require("../Assets/Images/hair_salon_logo1.png").default}/>
            </Navbar.Brand>
            <Navbar.Toggle aria-controls="basic-navbar-nav" />
            <Navbar.Collapse id="basic-navbar-nav">
              <Nav className="mr-auto">
                <Nav.Link href="#home">Home</Nav.Link>
                <Nav.Link href="#services">Services</Nav.Link>
                <Nav.Link href="#stylists">Stylists</Nav.Link>
                <Nav.Link href="#appointment">Appointment</Nav.Link>
                <Nav.Link href="#contact">Contact</Nav.Link>
              </Nav>
              <Nav className="ml-auto">
                <Nav.Link href="#twitter" id="twitter"><TwitterOutlined/></Nav.Link>
                <Nav.Link href="#facebook" id="facebook"><FacebookOutlined/></Nav.Link>
                <Nav.Link href="#youtube" id="youtube"><YoutubeOutlined/></Nav.Link>
                <Nav.Link href="#pause" id="pause"><PauseOutlined/></Nav.Link>
                <Nav.Link href="#search" id="search"><SearchOutlined/></Nav.Link>
              </Nav>
            </Navbar.Collapse>
          </Navbar>
        </Header>
        <Content>
          function onChange(a, b, c) {
            console.log(a, b, c);
          }

          const contentStyle = {
            height: '160px',
            color: '#fff',
            lineHeight: '160px',
            textAlign: 'center',
            background: '#364d79',
          };

          ReactDOM.render(
            <Carousel afterChange={onChange}>
              <div>
                <h3 style={contentStyle}><!--image-1--></h3>
              </div>
              <div>
                <h3 style={contentStyle}><!--image-2--></h3>
              </div>
              <div>
                <h3 style={contentStyle}><!--image-3--></h3>
              </div>
              <div>
                <h3 style={contentStyle}><!--image-4--></h3>
              </div>
            </Carousel>,
            mountNode,
          );
        </Content>
        <Footer>
        </Footer>
    </Layout>
    );
  }
}
export default App;
1 Answers

Use a functional component instead of a class component.

Create a array "imgCarousel" for example.

Your images are local so create a folder "images" in "public" and paste them inside. Your array will look like this:

const imgCarousel = [
  {
    id: 1,
    uri: "images/image1.jpg"
  },
  {
    id: 2,
    uri: "images/image2.jpg"
  },
  {
    id: 3,
    uri: "images/image3.jpg"
  },
  {
    id: 4,
    uri: "images/image4.jpg"
  }
];

Then just make a map and specify the image link using process.env.PUBLIC_URL :

{
  imgCarousel.map (img => (
   <div key={img.id} style={contentStyle}>
    <img src={process.env.PUBLIC_URL + img.uri} alt="" />     // here
   </div>
))} 

Demo: stackblitz

Related