将多层添加到react-konva容器时出现的问题

问题描述 投票:0回答:1

由于Dom元素,我在使用react-konva时遇到问题。当我尝试添加功能以根据dom的大小调整konva屏幕的大小时。添加图层后,我抛出了此错误。

const checkSize = contentRect => {
   const { width, height } = contentRect;
   stage.attrs({
     width,
     height
   });
   stage.batchDraw();
 };

export default class Review extends React.Component {
   constructor(props) {
    super(props);
     this.state = { stage: {} };
  }
  componentDidMount() {
    const { values, colorValue } = this.props;
     imageValue = values;
   colourValue = colorValue;
    this.state.stage = new Konva.Stage({
      container: "container",
      width: 0,
     height: 0
    });
    window.addEventListener("resize", checkSize);
  }

在那个阶段抛出一个错误

   layer = new Konva.Layer();
   stage.add(layer);

错误消息:TypeError:阶段未定义

javascript reactjs konvajs react-konva
1个回答
0
投票

我想您在保存该变量时需要从this.state中获取该变量:

const checkSize = contentRect => {
   const { width, height } = contentRect;
   this.state.stage.attrs({
     width,
     height
   });
   this.state.stage.batchDraw();
 };
© www.soinside.com 2019 - 2024. All rights reserved.