Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Three.js inside Gatsby?

I've installed react-three-fiber and three packages. I'm following this tutorial, but I have doubts of where to put this line:

const rootElement = document.getElementById("root");

Also, I'm starting to receive this error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

My index.js:

import React, { Children, useRef, useState } from "react"

import {  Link } from "gatsby"

import { Canvas} from 'react-three-fiber'

import Layout from "../components/layout"

const IndexPage = () => {

  return(

    <Layout>

      <div>

        <h1>Hi</h1>

      </div>

      <canvas>

        <Children></Children>

      </canvas>

    </Layout>

    )

}

const rootElement = document.getElementById("root");

export default IndexPage

Any ideas?

like image 773
dawn Avatar asked Dec 22 '22 19:12

dawn


1 Answers

Make sure you have installed both three and react-three-fiber.

npm install three react-three-fiber 

Then in your gatsby page component just import Canvas from react-three-fiber before using it in your JSX.

import React from "react"
import { Canvas} from 'react-three-fiber'

import Layout from "../components/layout"

const IndexPage = () => (
  <Layout>
    <Canvas />
  </Layout>
)

export default IndexPage

Regarding const rootElement = document.getElementById("root"); :

Although Gatsby is built with React, it doesn't require you to select a root element in order to render your app. If this sounds confusing, you should spend a little bit of time to take a read of the Gatsby docs.


If you were to build the example from the react-three-fiber docs in Gatsby, it would look something like this.

import React, { useRef, useState } from "react"
import { Canvas, useFrame } from "react-three-fiber"

const Box = props => {
  // This reference will give us direct access to the mesh so we can animate it
  const mesh = useRef()

  // Set up state for the hovered and active state
  const [hovered, setHover] = useState(false)
  const [active, setActive] = useState(false)

  // Rotate mesh every frame, this is outside of React without overhead
  useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01))

  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
      onClick={e => setActive(!active)}
      onPointerOver={e => setHover(true)}
      onPointerOut={e => setHover(false)}
    >
      <boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
      <meshStandardMaterial
        attach="material"
        color={hovered ? "hotpink" : "orange"}
      />
    </mesh>
  )
}

const IndexPage = () => (
  <Canvas>
    <ambientLight />
    <pointLight position={[10, 10, 10]} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>
)

export default IndexPage

Edit gatsby-react-three-fibre-example

like image 131
ksav Avatar answered Jan 31 '23 13:01

ksav