React + Three.js: Creating Immersive 3D Experiences on the Web
Bruno Freitas
Senior React Developer | Full Stack Developer | JavaScript | TypeScript | Node.js
The web is evolving, and users now expect more interactive and visually stunning experiences. With the power of React and Three.js, developers can bring 3D graphics into web applications, creating immersive and engaging experiences for users. In this article, we'll explore how you can integrate Three.js with React and why this combination is a game-changer for web development.
Why React and Three.js?
React is a powerful library for building dynamic UIs, while Three.js is the leading framework for rendering 3D graphics in the browser using WebGL. Together, they provide:
Getting Started with React and Three.js
To begin, install Three.js in your React project:
npm install three
You can start by creating a simple Three.js scene inside a React component:
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
const ThreeScene = () => {
const mountRef = useRef(null);
useEffect(() => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
mountRef.current.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
const animate = () => {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
}, []);
return <div ref={mountRef} />;
};
export default ThreeScene;
Enhancing the Experience with React Three Fiber
Manually handling the Three.js lifecycle in React can be complex. Instead, we can use React Three Fiber (R3F), a React renderer for Three.js, which simplifies the integration and makes it more React-friendly. To install it:
npm install @react-three/fiber
Then, you can rewrite the same scene using R3F:
import { Canvas } from '@react-three/fiber';
import { OrbitControls } from '@react-three/drei';
const ThreeJSScene = () => {
return (
<Canvas>
<ambientLight />
<mesh>
<boxGeometry />
<meshStandardMaterial color="blue" />
</mesh>
<OrbitControls />
</Canvas>
);
};
export default ThreeJSScene;
Use Cases of React + Three.js
This combination is widely used in:
Final Thoughts
By integrating React with Three.js (or React Three Fiber), developers can create next-level web experiences that captivate users. Whether you're building an interactive product viewer, a 3D data visualization, or a VR application, this powerful combination opens up endless possibilities.
What are your thoughts on using Three.js with React? Have you built any cool 3D projects? Let’s discuss in the comments! ??
Senior Data Engineer | Azure | AWS | GCP | SQL | Python | PySpark | Big Data | Airflow | Oracle | Data Warehouse | Data Lake
2 周Awesome article, Bruno! React and Three.js together truly open up endless possibilities for immersive 3D web experiences. I appreciate how you highlighted their synergy, like React's modularity paired with Three.js's rendering power. It’s exciting to see how tools like React Three Fiber simplify this integration. Thanks for sharing such valuable insights—it's inspiring for anyone exploring 3D web development!
QA | Software Quality | Test Analyst | CTFL | CTFL-AT
2 周Bruno Freitas great content!
Senior Front-end Developer | React - NextJS - Typescript - NodeJS - AWS
2 周Great article! React and Three.js are definitely a powerful combination. Appreciate the clear examples.
Senior .NET Software Engineer | Senior Full Stack Developer | C# | .Net Framework | Azure | React | SQL | Microservices
2 周Great article!
Android Developer | Mobile Software Engineer | Kotlin | Jetpack Compose | XML
2 周Great content!! Thanks for sharing!!