0

function getElement(){
  const el = document.createElement('canvas');
  el.drawImage(...);
  ...
  return el;
}

function App() {
  const canvasRef = React.useRef(null);

  React.useEffect(()=>{
    const canvasEl = getElement(); // It returns HTMLCanvasElement
    document.body.append(canvasEl); // It works but it isn't my desired
    canvasRef.current = canvasEl; // It doesn't work
  }, []);
  
  return (
    <div><canvas ref={canvasRef} /></div>
  )

}

It is simplified code so my actual code is much more complicated.

How can I inject HTMLCanvasElement into ref?

6
  • Isn't the ref automatically getting assigned? You don't have to manually update the current property Commented Dec 17, 2021 at 7:02
  • which canvas' ref are you planning to store in the ref? the one from getElement or the one in the JSX? Commented Dec 17, 2021 at 7:03
  • @RameshReddy getElement doesn't return JSX, it return HTMLCanvasElement. but I would like to render getElement() using ref. Commented Dec 17, 2021 at 7:09
  • That's not my question. You have two canvas elements. Which of these are you planning to store in the ref? Commented Dec 17, 2021 at 7:11
  • Are you planning to render the canvas element from getElement() inside the div? Commented Dec 17, 2021 at 7:12

2 Answers 2

2

Update:

This is not recommended but you can append the HTML canvas element like this:

Edit interesting-kowalevski-7koui

function getCanvas() {
  const canvas = document.createElement("canvas");
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = "green";
  ctx.fillRect(10, 10, 100, 100);
  return canvas;
}

export default function App() {
  const divRef = useRef(null);

  useEffect(() => {
    divRef.current.appendChild(getCanvas());
  }, []);

  return <div ref={divRef} />;
}

You don't have to create a new canvas element as you already have a canvas element in your JSX.

Once the ref is assigned, you can update it as you want.

Edit sharp-monad-r1fgd

function updateCanvas(canvas) {
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = "green";
  ctx.fillRect(10, 10, 100, 100);
}

export default function App() {
  const canvasRef = useRef(null);

  useEffect(() => {
    updateCanvas(canvasRef.current);
  }, []);

  return (
    <div>
      <canvas ref={canvasRef} />
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if I can not touch updateCanvas()? I can touch only returned a canvas element.
@kyun I'm not sure why you cannot touch updateCanvas(). Check my updated answer.
2

You can do something like this

import React from 'react';

function App() {
  const canvasRef = React.useRef(null);

  function addImageToElement() {
    const el = canvasRef.current;
    const ctx = el.getContext('2d');

    var img = new Image();
    img.src = 'https://picsum.photos/300';
    img.addEventListener(
      'load',
      () => ctx.drawImage(img, 10, 10, 150, 180),
      false
    );
  }

  React.useEffect(() => {
    addImageToElement();
  }, []);

  return (
    <div>
      <canvas ref={canvasRef} />
    </div>
  );
}

export default App;

Here is the live example: https://stackblitz.com/edit/react-fajhkj

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.