4

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?

ksav
  • 20,015
  • 6
  • 46
  • 66
dawn
  • 1,327
  • 6
  • 19
  • 39

1 Answers1

11

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

ksav
  • 20,015
  • 6
  • 46
  • 66
  • Thanks. I also found this video which doesn't used that "rootElement". https://www.youtube.com/watch?v=1rP3nNY2hTo – dawn Feb 20 '20 at 07:25
  • Unfortunately the tutorial you are following assumes a lot of existing knowledge with react and threejs. On top of that, it doesn't mention gatsby at all. – ksav Feb 20 '20 at 07:32