由于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();
};