React-three-fiber: useFrame drops "TypeError: subscribe is not a function" on Next.js

Created on 9 Mar 2020  路  5Comments  路  Source: pmndrs/react-three-fiber

When I try to use this line of code in a Next.js app, it drops an error:

  useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01))
TypeError: subscribe is not a function

code sandbox

Most helpful comment

see: https://github.com/react-spring/react-three-fiber#hooks

youre outside of the canvas context, so that useframe makes no sense. the doesnt canvas even exist at that point. or what if you have 2 canvases or more. it needs to be within to know the context.

All 5 comments

see: https://github.com/react-spring/react-three-fiber#hooks

youre outside of the canvas context, so that useframe makes no sense. the doesnt canvas even exist at that point. or what if you have 2 canvases or more. it needs to be within to know the context.

@drcmda could ya pls provide any example with propely working canvas? idk how to make it work properly ;-;

again, you are using these hooks outside of the canvas. you can't, they are reliant on context. it's explained in the link i've sent you.

this:

useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));

  return (
    <Canvas gl2>

is not possible.

but this:

function Mesh() {
  const ref = useRef()
  useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));
  return <mesh ref={ref}> ...
}

function App() {
  return (
    <Canvas gl2>
      <Mesh />

will work.

it is just like context in react. the useContext hook can only be used within a provider.

@drcmda oh, ok, now understand it

sorry for my stupidity :(

nah, dont worry :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LordOkami picture LordOkami  路  8Comments

gaearon picture gaearon  路  3Comments

dghez picture dghez  路  5Comments

AndrewPrifer picture AndrewPrifer  路  6Comments

francopetra picture francopetra  路  3Comments