Xiufen Xu
Xiufen Xu

Reputation: 531

How can I insert the <carousel> element into react.js

I am writing an website, which is based on react.js. I want the background image slide automatically in my index page and I import the element {carousel} from Ant Design, which has the implementation of how to use carousel. I have the code which will route to every component. Now, I am wondering how can I insert the {carousel} into the index page. I have the following code:

/* eslint no-unused-vars: "off" */

import React from 'react';
import { render } from 'react-dom';
import { Router, Route, browserHistory } from 'react-router'

import Root from './Root/Root'
import MainLayout from './MainLayout/MainLayout'
import MyRoot from './MyRoot/MyRoot'
import CreateNew from './CreateNew/CreateNew'
import './index.css'
import { Carousel } from 'antd';

// see https://github.com/ReactTraining/react-router
render((

  // <Carousel autoplay>
  //   <div><image>1</image></div>
  //   <div><image>2</image></div>
  //   <div><image>3</image></div>
  //   <div><image>4</image></div>
  // </Carousel>, mountNode;

  <Router history={browserHistory}>
    <Route component={MainLayout}>
      <Route path="/" component={Root}/>
      <Route path="/myitinerary" component={MyRoot}/>
      <Route path="/createnew" component={CreateNew}/>
    </Route>
  </Router>
), document.getElementById('root'));

Honestly, I have no idea why I did this, I am just guessing. Or do I need to create another component to hold the carousel information? Or is there any other easy-understand way to do this. Thank you so much!

Upvotes: 1

Views: 1758

Answers (2)

Poria Sobhanlo
Poria Sobhanlo

Reputation: 29

use a import { Carousel } from 'antd'; on top component

Upvotes: 1

Sergio
Sergio

Reputation: 28837

React Router will call diferent components depending on the path. So, in your example <Route path="/myitinerary" component={MyRoot}/> that component MyRoot needs to be defined somewhere.

And it looks like its coming from here: import MyRoot from './MyRoot/MyRoot'

So, inside that file you could have:

class MyRoot extends React.Component {
    render(){
       return (
          <Carousel autoplay>
            <div><h3>1</h3></div>
            <div><h3>2</h3></div>
            <div><h3>3</h3></div>
            <div><h3>4</h3></div>
          </Carousel>
        );
    }
}
export default MyRoot;

And that would pass the carousell inside the component. A example of just the caroussel would be:

const { Carousel } = antd;

ReactDOM.render(
  <Carousel autoplay>
    <div><h3>1</h3></div>
    <div><h3>2</h3></div>
    <div><h3>3</h3></div>
    <div><h3>4</h3></div>
  </Carousel>
, document.getElementById('app'));
.ant-carousel .slick-slide {
  text-align: center;
  height: 160px;
  line-height: 160px;
  background: #364d79;
  color: #fff;
  overflow: hidden;
}
<link href="https://ant.design/index-1.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://npmcdn.com/antd/dist/antd.js"></script>
<div id="app"></div>

Upvotes: 1

Related Questions