javascript - 向 react-konva 容器添加多个层时出现问题
问题描述
由于 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:阶段未定义
解决方案
我想您需要从this.state
那里保存该变量:
const checkSize = contentRect => {
const { width, height } = contentRect;
this.state.stage.attrs({
width,
height
});
this.state.stage.batchDraw();
};
推荐阅读
- sql - 在 SQL Server 中组合查询输出以制作一张汇总表
- excel - 如何在名称列中分隔电话号码
- solana - 如何在 solana 网络上更新代币名称和小数点?
- wordpress - Woocommerce Rest api 不适用于亚马逊(AWS Bitnami 实例)
- mysql - 用 mySQL 计算“子列”
- php - PHP postgres 故障转移
- wcf - 如何在 Visual Studio 2019 中将 WCF 服务添加到 .NET Core 项目?
- javascript - 从父到子路由的 navigateByUrl 不起作用
- html - 如何在不重叠内容的情况下创建可重复的标题?
- c# - MVVM 中的关注点分离