Skip to main content

Implement carousel with React JS in SPFx

Implement a Carousel with React JS in SPFx

Implement a Carousel with React JS in SPFx

Author: Vishal Thakur

If you want to implement a carousel like the one shown below, you're in the right place. In this blog, I'll show you how to create a carousel using Bootstrap and React JS in SharePoint/Office 365 SPFx.

Introduction

Implementing a carousel in React JS is not a big deal if you follow this guide. React JS has become very popular, and there are many plugins available to fulfill client requirements. In this blog, I'll walk you through the process of creating a carousel using Bootstrap and React JS in SharePoint/Office 365 SPFx.

Steps to Follow

It's as simple as making coffee. You just need to execute a few npm commands. You can run these commands directly using Node.js Command Prompt or Visual Studio Code IDE by opening the terminal from the top menu.

  • Create a folder in your development directory and open it in Visual Studio Code.
  • Copy and paste the following command into the terminal window:
yo @microsoft/sharepoint

After running the command, follow the prompts and select React JS as the framework. Once the package is created, run the following commands to install the required dependencies:

npm i --save react-bootstrap

This will install the package combination of Bootstrap and React JS.

npm install bootstrap@4 --save
npm install @types/bootstrap@4 --save

The above commands resolve some CSS-related issues that I faced. After running these commands, the component should work fine.

npm install jquery --save
npm install @types/jquery --save

These commands resolve the dependency of jQuery. Now, everything is set up, and it's time for the final code.

Code

Open your TSX file and add the following lines at the top of the import section:

import Carousel from 'react-bootstrap/Carousel';
import "bootstrap/dist/css/bootstrap.css";
import * as jQuery from 'jquery';

Now, copy the following code and paste it into your render section:

<Carousel>
  <Carousel.Item>
    <img className="d-block w-100" src="[Your Image URL]" alt="First slide" />
    <Carousel.Caption>
      <h3>First slide label</h3>
      <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
    </Carousel.Caption>
  </Carousel.Item>
  <Carousel.Item>
    <img className="d-block w-100" src="[Your Image URL]" alt="Second slide" />
    <Carousel.Caption>
      <h3>Second slide label</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </Carousel.Caption>
  </Carousel.Item>
  <Carousel.Item>
    <img className="d-block w-100" src="[Your Image URL]" alt="Third slide" />
    <Carousel.Caption>
      <h3>Third slide label</h3>
      <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
    </Carousel.Caption>
  </Carousel.Item>
</Carousel>

You can replace the image paths with actual URLs. If you encounter issues, ensure the binary code is in a single line.

Conclusion

Finally, your carousel should look like the image below. I hope this guide helps you implement a carousel in your SPFx project. In my next blog, I'll show you how to implement dynamic and fancy sliders.

Please share your feedback to help me improve my blogs. For more tutorials, visit my blog: The Ultimate Resources.

Comments

  1. Hi,
    I am getting these errors
    [21:58:18] Error - [tsc] node_modules/@restart/ui/esm/types.d.ts(5,1): error TS1128: Declaration or statement expected.
    [21:58:18] Error - [tsc] node_modules/@restart/ui/esm/types.d.ts(5,13): error TS1005: ';' expected.

    ReplyDelete

Post a Comment

Popular posts from this blog

Power Apps modern driven app Interview Question and answer

Power Apps Modern Driven App: Questions and Answers Power Apps Modern Driven App: Questions and Answers Power Apps modern driven apps are low-code/no-code platforms that enable users to create custom applications. Below are some common questions and detailed answers to help you understand their capabilities and applications. Question 1: What is a Power Apps modern driven app? Answer: A Power Apps modern driven app is a low-code/no-code application development platform provided by Microsoft. It allows users to create custom applications that can run on various devices and platforms without extensive coding. These apps are built using a visual interface and can integrate with different data sources. Question 2: What are the key components of a Power Apps modern driven app? Answer: The key components of a Power Apps modern driven app are: Screens: Serve as the user interface for the app, including layouts, ...

SPFX Interview question for 2023

SPFx Interview Questions for 2023 SPFx Interview Questions for 2023 Author: Vishal Thakur Question 1: What is SharePoint Framework (SPFx)? Answer: SharePoint Framework (SPFx) is a development model introduced by Microsoft for creating client-side web parts and extensions for SharePoint Online and SharePoint 2019. It is based on modern web technologies like JavaScript, TypeScript, and React, providing a rich and responsive user experience. Question 2: What are the key advantages of using SPFx for SharePoint development? Answer: SPFx offers several advantages, such as: Responsive and mobile-ready web parts and extensions. Seamless integration with SharePoint data and services. Support for modern web development practices and tools. Easy deployment and hosting options. Enhanced security and isolation through the SharePoint app model. Question 3: Can you explain ...

Interview Questions of SPFx SharePoint

SPFx Interview Questions and Scenarios SPFx Interview Questions and Scenarios By Vishal Thakur What is SPFx? The SharePoint Framework (SPFx) is a web part model that provides full support for client-side SharePoint development. It integrates seamlessly with SharePoint data and extends Microsoft Teams. SPFx allows developers to use modern web technologies and tools in their preferred development environment to build responsive and mobile-ready experiences. Scenario-Based Questions Scenario 1: External API Integration Scenario: Your team is developing an SPFx web part that needs to retrieve data from an external API and display it on a SharePoint site. The API requires authentication using OAuth 2.0. The web part should also allow users to refresh the data manually. Question: How woul...