【问题标题】:Issue when adding multiple layers to react-konva container向 react-konva 容器添加多个层时出现问题
【发布时间】:2019-09-29 09:11:48
【问题描述】:

由于 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】:

    我猜你需要从this.state 中取出那个变量,因为你把它保存在那里:

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

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2021-07-29
      • 2018-03-13
      • 2021-06-10
      • 2012-04-07
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多