简体   繁体   English

React - 无法访问道具

[英]React - Can't access props

I created a React Component which has a canvas and a button .我创建了一个 React 组件,它有一个canvas和一个button When the component mounts, an event listener is added to the button to draw something over the canvas.当组件挂载时,一个事件监听器被添加到button中以在 canvas 上绘制一些东西。 Also, this event listener is removed once it is executed.此外,一旦执行此事件侦听器,它就会被删除。 This Component has a prop attribute text .这个组件有一个 prop 属性text

<Canvas text="Hello"></Canvas>

On clicking the button, I get the following error.单击按钮时,我收到以下错误。 I'm unable to access the text prop.我无法访问text道具。

TypeError
Cannot read properties of undefined (reading 'text')

Component -零件 -

import React from "react";

class Canvas extends React.Component {
  componentDidMount() {
    const canvas = this.refs.canvas;
    const button = this.refs.button;
    const ctx = canvas.getContext("2d");
    
    function action() {
      console.log("HI")
      ctx.font = "40px Consolas";
      ctx.fillStyle = "#F5F5F5";
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      ctx.fillStyle = "#000000";
      ctx.fillText(this.props.text, 10, 40);
      button.removeEventListener("click", action);
    }
    
    button.addEventListener("click", action);
  }

  render() {
    return (
      <div>
        <canvas ref="canvas" width={300} height={300} />
        <button ref="button" style={{
          display: "block",
          margin: "auto",
          marginTop: "10px"
        }}>Show</button>
      </div>
    );
  }
}
export default Canvas;

According to https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#other_notes :根据https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#other_notes

When attaching a handler function to an element using addEventListener(), the value of this inside the handler will be a reference to the element.当使用 addEventListener() 将处理程序 function 附加到元素时,处理程序中 this 的值将是对该元素的引用。 It will be the same as the value of the currentTarget property of the event argument that is passed to the handler.它将与传递给处理程序的事件参数的 currentTarget 属性的值相同。

You should explicitly specify this when passing it.您应该在传递它时明确指定this

Try this:尝试这个:

import React from "react";

class Canvas extends React.Component {
   componentDidMount() {
   const canvas = this.refs.canvas;
   const button = this.refs.button;
   const ctx = canvas.getContext("2d");

   function action() {
     console.log("HI")
     ctx.font = "40px Consolas";
     ctx.fillStyle = "#F5F5F5";
     ctx.fillRect(0, 0, canvas.width, canvas.height);
     ctx.fillStyle = "#000000";
     ctx.fillText(this.props.text, 10, 40);
     button.removeEventListener("click", action);
   }
   const onButtonClicked = action.bind(this)

   button.addEventListener("click", onButtonClicked);
  }

 render() {
   return (
     <div>
       <canvas ref="canvas" width={300} height={300} />
       <button ref="button" style={{
         display: "block",
         margin: "auto",
         marginTop: "10px"
       }}>Show</button>
     </div>
   );
 }
}
export default Canvas;

The problem appears because you don't have access to context in the function.出现问题是因为您无权访问 function 中的上下文。 To avoid this, you can use .bind(this) .为避免这种情况,您可以使用.bind(this) Can you explain, why you need to delete the event listener after the first use?你能解释一下,为什么你需要在第一次使用后删除事件监听器?
This is not something very common.这不是很常见的事情。 My suggestion is to use onClick={action} .我的建议是使用onClick={action}

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

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