Blog post

How to build a React.js image viewer

Illustration: How to build a React.js image viewer

In this post, we provide you with a step-by-step guide on how you can deploy Nutrient’s React.js image viewer. React.js is a JavaScript library for building user interfaces, and according to the Stack Overflow 2021 Developer Survey, React is the most popular web framework.

What is a React image viewer?

A React image viewer lets you render and view images in a web browser without the need to download it to your hard drive or use an external application like an image reader.

Nutrient React image viewer

We offer a commercial React.js image viewer library that can easily be integrated into your web application. The React image viewer library supports rendering JPEG, PNG, and TIFF files in any modern browser and on any mobile device without any plugins. It offers developers a way to quickly embed a highly configurable image or PDF viewer with a beautiful UI in any web application.

  • A prebuilt and polished UI for an improved user experience
  • 15+ prebuilt annotation tools to enable document collaboration
  • Support for more file types with client-side PDF, MS Office, and image viewing
  • Dedicated support from engineers to speed up integration

Example of our React image viewer

To see our image viewer in action, upload a JPG, PNG, or TIFF file by selecting Choose Example > Open Document. Once your image is displayed in the viewer, you can try drawing freehand, adding a note, or applying a crop or an eSignature.

Requirements to get started

To get started, you’ll need:

  • The latest version of Node.js.

  • A package manager compatible with npm. This guide contains usage examples for Yarn and the npm client (installed with Node.js by default).

Setting up a new React project with Vite

  1. To get started, create a new React project using Vite:

# Using Yarn
yarn create vite pspdfkit-react-example --template react

# Using npm
npm create vite@latest pspdfkit-react-example -- --template react
  1. Change to the created project directory:

cd pspdfkit-react-example

Adding Nutrient to your project

  1. Add the Nutrient dependency:

yarn add pspdfkit
npm install pspdfkit
  1. Copy the Nutrient Web SDK library assets to the public directory:

cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib

The above code will copy the pspdfkit-lib directory from within node_modules/ into the public/ directory to make it available to the SDK at runtime.

  1. Make sure your public directory contains a pspdfkit-lib directory with the Nutrient library assets.

Displaying an image

  1. Add the image you want to display to the public directory. You can use our demo image as an example.

  2. Add a component wrapper for the Nutrient library and save it as components/ViewerComponent.jsx:

import { useEffect, useRef } from 'react';

export default function ViewerComponent(props) {
	const containerRef = useRef(null);

	useEffect(() => {
		const container = containerRef.current;
		let PSPDFKit;
		(async function () {
			PSPDFKit = await import('pspdfkit');

			PSPDFKit.unload(container); // Ensure there's only one PSPDFKit instance.

			await PSPDFKit.load({
				// Container where Nutrient should be mounted.
				container,
				// The document to open.
				document: props.document,
				// Use the public directory URL as a base URL. Nutrient will download its library assets from here.
				baseUrl: `${window.location.protocol}//${
					window.location.host
				}/${import.meta.env.BASE_URL}`,
			});
		})();

		return () => PSPDFKit && PSPDFKit.unload(container);
	}, []);

	return (
		<div
			ref={containerRef}
			style={{ width: '100%', height: '100vh' }}
		/>
	);
}
  1. Include the newly created component in App.jsx:

// src/App.jsx

import ViewerComponent from './components/ViewerComponent';

function App() {
	return (
		<div className="App">
			<div className="PDF-viewer">
				<ViewerComponent document={'image.png'} />
			</div>
		</div>
	);
}

export default App;
  1. Your project structure should now look like this:

pspdfkit-react-example/
├── public/
│   ├── pspdfkit-lib/
│   └── image.png
├── src/
│   ├── components/
│   |   └── ViewerComponent.jsx
|   └── App.jsx
├── package.json
└── yarn.lock
  1. Start the app and run it in your default browser:

# Using Yarn
yarn dev

# Using npm
npm run dev

Resulting page

Adding even more capabilities

Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React.js guides:

Conclusion

You should now have our React image viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our support team for help.

We created similar how-to blog posts using different web frameworks and libraries:

To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.

FAQ

What is a React image viewer? A React image viewer allows you to render and view images directly in a web browser without needing additional software.
Why use Nutrient for a React image viewer? Nutrient offers a feature-rich, customizable image viewer that supports various image formats and integrates seamlessly into React applications.
How do I integrate Nutrient into my React project? Install Nutrient via npm or Yarn, add it to your project’s public directory, and use a component wrapper to display images.
What image formats are supported by Nutrient’s React image viewer? Nutrient supports JPEG, PNG, and TIFF image formats.
Can I customize the Nutrient React image viewer? Yes. Nutrient’s React image viewer is highly customizable, allowing you to adjust the UI and functionality according to your needs.
Author
Hulya Masharipov
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Free trial Ready to get started?
Free trial