# React 360 Viewer, Image Zoom, Video Gallery

[Sirv Media Viewer](/help/sirv-media-viewer/overview/) is a powerful tool for showcasing rich media ([360 spins](/help/sirv-media-viewer/media-types/360-spin/), [image zoom](/help/sirv-media-viewer/media-types/image-zoom/) and [videos](/help/sirv-media-viewer/media-types/video-streaming/)) that plays great with React websites or apps.

Using an AI coding agent?

Install the [`sirv-media-viewer`](https://www.skills.sh/sirv/image-optimization-skill/sirv-media-viewer) skill, and your agent can add the viewer to your site. Ask in plain words, such as “Add a gallery with zoom and a [360 spin](/help/spin/getting-started/how-spin-files-are-created/) to the product page.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

[Sirv Media Viewer](/help/sirv-media-viewer/) also allows you to intelligently deliver [responsive images](/help/sirv-media-viewer/core-features/responsive-images/) to your visitors, by properly scaling and changing the image format on-the-fly, based on the user’s device/browser.

## Getting started

To utilize Sirv Media Viewer’s advanced features (dynamic image resizing, image zoom tiling and much more), first you should:

1.  [Create a Sirv account](https://app.sirv.com/signup) (or [log in](https://app.sirv.com/login)).
2.  [Upload media](/help/file-upload/upload-images/) to your Sirv account.

## Add Sirv embed markup

Add Sirv Media Viewer markup code - this example showcases a 360 spin, a zoomable image and a video in a gallery with thumbnails:

```html
<div class="Sirv">
  <div data-src="https://demo.sirv.com/example.spin"></div>
  <div data-src="https://demo.sirv.com/image.jpg" data-type="zoom"></div>
  <div data-src="https://demo.sirv.com/video.mp4"></div>
</div>
```

## Initialize Sirv JS

The following example uses the [document.createElement()](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement) method:

```js
loadSirv(components) {
  return new Promise(resolve => {
    const script = document.createElement("script");
    script.src = "https://scripts.sirv.com/sirvjs/v3/sirv.js";
    script.type = "text/javascript";
    script.async = true;
    if (components) {
      script.dataset.components = components;
    }
    script.onload = resolve;
    document.body.appendChild(script);
  });
}
```

Now call the function in React’s `componentDidMount` lifecycle method to initialize Sirv:

```js
componentDidMount() {
  this.loadSirv("spin, zoom, video").then(() => {
    window.Sirv.start()
  });
}
```

For functional components you can use the `UseEffect` hook. [Click here for a live sandbox](https://stackblitz.com/edit/smv-react-router?file=components%2FSmv.js). If you encounter flickering, try using `UseLayoutEffect` instead. Here’s a [demo](https://smv-react-router.stackblitz.io/layouteffect) and the [code](https://stackblitz.com/edit/smv-react-router?file=components%2FLayouteffect.js).

Load only the [Sirv Media Viewer components](/help/sirv-media-viewer/overview/#load-smaller-sirvjs-version) you need by passing a string as a parameter in the `loadSirv` function. We’re loading the `spin`, `zoom` and `video` components in this example.

Calling the function without parameters will load the full Sirv JS.

Here’s the end result:

## Using the Sirv API and events

Use `window.Sirv` to control all aspects of Sirv Media Viewer and its components. Here’s a full reference for all API methods and events:

-   [Sirv JS API methods](/help/sirv-media-viewer/advanced-implementation/api/)
-   [Sirv JS events](/help/sirv-media-viewer/advanced-implementation/events/)

### Loading Sirv JS on event

Sometimes you might want to initialize Sirv only after a specific event occurs. Here’s how to do it with React’s `onClick` event:

**1.** Set the [autostart option](/help/sirv-media-viewer/overview/#autostart) to off in your Sirv embed:

```jsx
<div className="Sirv off" data-options="autostart:off">
  <div data-src="https://demo.sirv.com/tshirt-aqua.spin"></div>
</div>
```

**2.** Create a new function, let’s name it `startSirv`:

```js
startSirv() {
  if (typeof window.Sirv === "undefined") {
    this.loadSirv("spin").then(() => {
      window.Sirv.start(".off");
    });
  } else {
    window.Sirv.start(".off");
  }
}
```

**3.** Call it using the `onClick` event handler:

```jsx
<button onClick={this.startSirv.bind(this)}>Start</button>
```

And we’re done:

### Custom navigation

The next example will to add a couple of buttons which can change the image currently shown.

Add some Sirv markup:

```jsx
<!-- Sirv embed code -->
<div className="Sirv custom" data-options="arrows:false;thumbnails.enable:false;">
  <div data-src="https://demo.sirv.com/demo/apt/01.jpg" data-type="zoom"></div>
  <div data-src="https://demo.sirv.com/demo/apt/02.jpg" data-type="zoom"></div>
  <div data-src="https://demo.sirv.com/demo/apt/03.jpg" data-type="zoom"></div>
</div>
```

```js
previous() {
  if (window.Sirv.viewer.getInstance(".custom").isReady()) {
    window.Sirv.viewer.getInstance(".custom").prev();
  }
}
next() {
  if (window.Sirv.viewer.getInstance(".custom").isReady()) {
    window.Sirv.viewer.getInstance(".custom").next();
  }
}
```

Now, let’s use the `onClick` event handler to bind buttons to these functions:

```jsx
<div className="buttons">
  <button onClick={this.previous.bind(this)}>⬅ Previous</button>
  <button onClick={this.next.bind(this)}>Next ➡</button>
</div>
```

Here’s the end result, with Previous and Next buttons:

As you can see, it’s easy to add responsive images, 360 spins, videos and image zooms in your React website or app.
