I'm trying to make a React component that renders a three.js scene. However, whenever I try mounting the component instead of seeing any sort of scene being rendered, I just see the text [object HTMLCanvasElement] being shown.
Here's my component:
import React from 'react';
import * as THREE from 'three';
class Cube extends React.Component {
constructor() {
super();
this.animate = this.animate.bind(this);
this.scene = new THREE.Scene();
this.geometry = new THREE.BoxGeometry(200, 200, 200);
this.material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
});
this.mesh = new THREE.Mesh(this.geometry,this.material);
this.scene.add(this.mesh);
this.renderer = null;
this.camera = null;
}
render() {
if (typeof window !== 'undefined') {
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
this.camera.position.z = 1000;
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
return (
<div dangerouslySetInnerHTML={{__html: this.renderer.domElement}}></div>
);
} else {
return null;
}
}
}
export default Cube;
I got the code from the three npm package page and tried to convert it into a React component. What am I doing wrong that is making the scene not render?