Skip to content

retrieve p5 from parent component as ref #540

@EricWVGG

Description

@EricWVGG

Is there any way to pass p5 back to the parent component as a ref? I'm trying to accomplish something like this:

const sketch: Sketch = (p5) => {
  p5.setup = () => //...

  p5.updateWithProps = (props: any) => {
    if( !props.ref?.current ) {
      props.ref.current = p5
    }
  }
}

const Fx = () => {
  const p5ref = useRef<P5CanvasInstance>(null)
  return (
    <>
      <P5Canvas sketch={sketch} ref={p5ref} />
      <button onClick={() => p5ref.current?.clear()} >Clear</button>
    </>
  )
}

Every time I try this, I start getting wild errors regarding Named export 'GIFEncoder' not found. The requested module 'gifenc' is a CommonJS module

(If I'm not nuts about this idea, I think it would be nice if this could exist in the constructor, where P5Canvas automatically initializes a ref when one is detected, rather than clumsily putting that in updateWithProps.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions