React + Three.js: Creating Immersive 3D Experiences on the Web

React + Three.js: Creating Immersive 3D Experiences on the Web


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:

  • Declarative UI Management: React's component-based architecture makes it easy to structure complex scenes.
  • Efficient Rendering: Using React’s Virtual DOM ensures smooth updates to 3D elements.
  • Modularity: Reusable components simplify the development of complex 3D interfaces.

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:

  • Product Showcases: Allow users to interact with 3D product models.
  • Data Visualization: Display complex datasets in a more intuitive 3D space.
  • Virtual Reality & Games: Develop immersive experiences using WebXR.
  • Architectural Visualization: Showcase 3D designs interactively.

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! ??


Matheus Teixeira

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!

回复
Cristiane E. Framil Fernandes

QA | Software Quality | Test Analyst | CTFL | CTFL-AT

2 周

Bruno Freitas great content!

回复
Fabricio Dorneles

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.

回复
Mauro Marins

Senior .NET Software Engineer | Senior Full Stack Developer | C# | .Net Framework | Azure | React | SQL | Microservices

2 周

Great article!

回复
Gabriel Levindo

Android Developer | Mobile Software Engineer | Kotlin | Jetpack Compose | XML

2 周

Great content!! Thanks for sharing!!

回复

要查看或添加评论,请登录

Bruno Freitas的更多文章

社区洞察