question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use non-three.js elements like <div> inside a <Canvas>?

See original GitHub issue

Again i’m a reconciler newcomer, but these lines look suspect in the createInstance method:

https://github.com/drcmda/react-three-fiber/blob/master/src/reconciler.js#L129-L130

    const target = catalogue[name] || THREE[name]
    instance = Array.isArray(args) ? new target(...args) : new target(args)

Two things seem to happen. First, the error itself seems to be getting eaten somewhere, but I’m not sure where. All that’s logged is

The above error occurred in the <ul> component:
    in ul (created by SelectableMenu)

But the error itself seems to be logged after that statement:

Uncaught TypeError: target is not a constructor

This error itself isn’t this most obvious to debug.

Why would I want to put a <div> inside a <Canvas> you ask? I’m using react-portal to make my 3d menu element also be able to render a DOM element, and have both the 3d element and GUI element use the same props. It’s pretty convenient.

That doesn’t seem compatible with this library. Do you have a suggestion for another approach?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
drcmdacommented, Apr 11, 2019

useThree is context based, it only works inside canvas. But it goes deeper, generally the real three canvas isn’t created in line with the Canvas component. Just because Canvas has rendered and the parent gets its useEffect ping doesn’t mean that the three js stuff is ready yet. React decides when it will render, and it seems to do it async by default. So the only safe way is to pass the camera up when it becomes available.

But you still have multiple options here.

  1. Canvas has a onCreated event that gives you the cam (+ state)
function Parent() {
  return <Child onCreated={({ camera }) => console.log(camera)} />
}

function Child(props) {
  return <Canvas {...props} />
}
  1. Canvas can render a function child that gives you the cam (+ state)
<Canvas>
  {({ camera }) => ... }
  1. Create and manage your own camera: https://github.com/drcmda/react-three-fiber#heads-up-display-rendering-multiple-scenes
0reactions
drcmdacommented, Jun 21, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use non-three.js elements like <div> inside a <Canvas>?
I'm using react-portal to make my 3d menu element also be able to render a DOM element, and have both the 3d element...
Read more >
Unable to use <canvas> element inside <div> element
I'm not able to add <canvas> animation inside <div> element. With the current code, animation takes full page and I want to encapsulate ......
Read more >
HTML Canvas Tutorial - W3Schools
The HTML <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript). The <canvas> element is only a container...
Read more >
Getting Started with the Canvas - KIRUPA
Get your feet wet using the canvas element by looking at a simple example and the HTML, CSS, and JavaScript that goes with...
Read more >
HTML <canvas> Accessibility - Paul J. Adam
Canvas element must have an accessible name and description that matches the visible text and content inside the canvas drawing area. Canvas elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found