简体   繁体   English

声明的const变量在use中不可见

[英]Declared const variable not visible in useEffect

I am working through the following code: 我正在通过以下代码进行工作:

index.js

import React, {useRef, useState, useEffect} from "react";
import ReactDOM from "react-dom";
import AddAPhotoTwoTone from '@material-ui/icons/AddAPhotoTwoTone'
import backImg from "./background.png";

const Canvas = (props) => {
  const canvas = useRef(null);
  const image = useRef(null);
  const xLoc = useState(props.backImg.width/2)
  const yLocTop = useState(props.backImg.height/2)
  const yLocBottom = useState(props.backImg.height/2)

  useEffect(() => {
    const ctx = canvas.current.getContext("2d");
    image.current.onload = () => {
      ctx.drawImage(image.current, 0, 0);            
      ctx.font = "20px Courier";
      ctx.textAlign = "center";
      console.log(xLoc)
      ctx.fillText(props.textTop, xLoc, yLocTop);
      ctx.textAlign = "center";
      ctx.fillText(props.textBottom, xLoc, yLocBottom);
    };
  });

  useEffect(() => {
    const ctx = canvas.current.getContext("2d");
    ctx.drawImage(image.current, 0, 0);
    // const xLoc = canvas.current.width/2
    // const yLocTop = canvas.current.height*.95
    // const yLocBottom = canvas.current.height*0.05
    ctx.font = "20px Courier";
      ctx.textAlign = "center";
      ctx.fillText(props.textTop, xLoc, yLocTop);
      ctx.textAlign = "center";
      ctx.fillText(props.textBottom, xLoc, yLocBottom);
  });

  return (
    <div>
      <canvas ref={canvas} width={props.backImg.width} height={props.backImg.height} />
      <img ref={image} src={props.backImg} hidden/>
    </div>
  );
};


function App() {  
  return (
    <div className="App">  
      <Canvas textTop="TEST 123" textBottom="TEST 456" backImg={backImg} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Why is the console.log(xLoc) within first useEffect printing NaN ? 为什么console.log(xLoc)在第一次useEffect打印NaN

  useEffect(() => {
    const ctx = canvas.current.getContext("2d");
    image.current.onload = () => {
      ctx.drawImage(image.current, 0, 0);            
      ctx.font = "20px Courier";
      ctx.textAlign = "center";
      console.log(xLoc)
      ctx.fillText(props.textTop, xLoc, yLocTop);
      ctx.textAlign = "center";
      ctx.fillText(props.textBottom, xLoc, yLocBottom);
    };
  });

Sandbox code is here 沙盒代码在这里

Edit: 编辑:

Even with properly configured useState , it returns NaN on console. 即使使用useState进行了正确配置,它useState在控制台上返回NaN。

useState() returns an array. useState()返回一个数组。 The first element is the state-value and the second is its state-updating function. 第一个元素是状态值,第二个元素是状态更新功能。 You need to destructure the output like so. 您需要像这样破坏输出。

Needs to be 需要是

const [xLoc, setxLoc] = useState(props.backImg.width/2)
const [yLocTop, setyLocTop] = useState(props.backImg.height/2)
const [yLocBottom, setyLocBottom] = useState(props.backImg.height/2)

Thus whatever you pass in to useState() will be the initial value. 因此,您传递给useState()都将是初始值。

Additionally. 另外。 The problem is that the props.backImg is simply a string. 问题是props.backImg只是一个字符串。 It does not have any object properties like .width , and .height . 它没有任何对象属性,例如.width.height To actually have access to those values, you need to first use that image as a src inside an img tag. 要实际访问这些值,您需要首先将该图像用作img标签内的src

Then the img tag has access to the onLoad event-listener. 然后, img标签可以访问onLoad事件侦听器。 With an event-handler, we can access the object properties mentioned. 使用事件处理程序,我们可以访问提到的对象属性。 In sense, we first need to render the image on the screen, but that doesn't happen until we render the Canvas component, so there is no need to pass in those props from App. 从某种意义上说,我们首先需要在屏幕上渲染图像,但是直到我们渲染Canvas组件时,这种情况才会发生,因此不需要从App中传递这些道具。 See sandbox for more details. 有关更多详细信息,请参见沙箱。

See sandbox: https://codesandbox.io/s/hardcore-night-djzmr 请参阅沙箱: https : //codesandbox.io/s/hardcore-night-djzmr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM