简体   繁体   English

在React.js中向组件添加多个形状?

[英]Adding multiple shapes to component in React.js?

I'm currently trying to do two things: 1. add multiple shapes to a React class 2. have one "selected" shape that will change color when selected. 我目前正在尝试做两件事:1.将多个形状添加到React类中2.具有一个“选定”形状,该形状在选定时会改变颜色。

I don't think I'm going about this in the right way. 我认为我的做法不正确。 First of all, I'm currently adding all the shapes to a canvas using a bunch of calls to fillRect , but that doesn't seem to fit the general React flow. 首先,我目前正在使用一堆调用fillRect将所有形状添加到画布上,但这似乎不适合一般的React流程。 Also, for now I'm just trying to hard code the state to be equal to one rectangle and then render that shape as a different color, but it's currently not even appearing on the screen (see screenshot). 另外,现在我只是尝试将状态硬编码为等于一个矩形,然后将该形状呈现为另一种颜色,但是目前它甚至没有出现在屏幕上(请参见屏幕截图)。

I would appreciate any advice with these two issues! 对于这两个问题,我将不胜感激!

Relevant code: 相关代码:

class Shapes extends React.Component {

getInitialState() {
    var highlightedShape = {x:200, y:200, width:60, height:40};
    highlightedShape.width = 70;
}

componentDidMount() {
    this.updateCanvas();
}

updateCanvas() {
    const ctx = this.refs.canvas.getContext('2d');
    ctx.fillRect(0,0, 100, 100);
    ctx.fillRect(70,70,90,90);
    //my attempt at 'highlighting' the state's selected shape:
    ctx.fillStyle="red";
    ctx.fillRect(this.state.highlightedShape.x, this.state.highlightedShape.y, this.state.highlightedShape.width, this.state.highlightedShape.height);

}
render() {
    return (
        <canvas ref="canvas" width={300} height={300}/>
    );
  }
}

module.exports = Shapes

Current output (selected shape doesn't even appear): 当前输出(所选形状甚至不会出现):

You need to return your initial state. 您需要返回初始状态。 Right now, your getInitialState call returns undefined , since you don't have a return statement. 现在,由于没有return语句,因此getInitialState调用返回undefined This means that in your updateCanvas() call, this.state.highlightedShape will be undefined (you should be getting errors in your console as well). 这意味着在您的updateCanvas()调用中, this.state.highlightedShape将是未定义的(您也应该在控制台中得到错误)。

Something like this: 像这样:

getInitialState() {
    var highlightedShape = {x:200, y:200, width:60, height:40};
    highlightedShape.width = 70;
    return { highlightedShape: highlightedShape };
}

Are you set on using canvas? 您打算使用画布吗? One alternative is to use svg which fits React better because it is DOM..y. 一种替代方法是使用svg,它更适合React,因为它是DOM..y。

https://jsfiddle.net/508n6skz/ https://jsfiddle.net/508n6skz/

var Shapes = React.createClass({
  render: function() {
    return (
    <svg width="500" height="500">
        <Rectangle x="10" y="10" width="100" height="200"></Rectangle>              
      <Rectangle x="20" y="30" width="50" height="100" selected="true"></Rectangle>
    </svg>);
  }
});

var Rectangle = React.createClass({
    _getColor: function(){
    return this.props.selected? 'red': 'blue';
  },
    render: function(){
    return (
    <rect fill={this._getColor()} x={this.props.x} y={this.props.y}
    width={this.props.width} height={this.props.height}></rect>
    );
  }
})

ReactDOM.render(
  <Shapes />,
  document.getElementById('container')
);

By using canvas in react it will simple cases not use the normal update flow with the render functions, since canvas content isn't supported by the shadow dom diffing. 通过使用画布react ,将简单的情况下不使用与渲染功能的正常更新流程,因为帆布内容不被影子DOM版本比较支持。 To overcome this you need to tell react how to do the component update with componentDidUpdate : 为了克服这个问题,您需要告诉react如何使用componentDidUpdate进行组件更新:

class Shapes extends React.Component {

getInitialState() {

    var highlightedShape = {x:200, y:200, width:60, height:40};
    // Remember to return the state object
    return {highlightedShape: highlightedShape} 
}

componentDidMount() {
    this.updateCanvas();
}

componentDidUpdate() {
    this.updateCanvas();
}

updateCanvas() {
    const ctx = this.refs.canvas.getContext('2d');
    ctx.fillRect(0,0, 100, 100);
    ctx.fillRect(70,70,90,90);
    //my attempt at 'highlighting' the state's selected shape:
    ctx.fillStyle="red";
    ctx.fillRect(this.state.highlightedShape.x, this.state.highlightedShape.y, this.state.highlightedShape.width, this.state.highlightedShape.height);

}
render() {
    return (
        <canvas ref="canvas" width={300} height={300}/>
    );
  }
}

module.exports = Shapes

To update the content you could depend on properties like this: 要更新内容,您可以依赖以下属性:

class Shapes extends React.Component {

getInitialState() {
    var highlightedShape = {x:200, y:200, width: this.props.shapeWidth, height:40};
}

componentWillReceiveProps() {
    // it's important to create a new object on state properties, because the state diffing is done by reference.
    this.setState(Object.assign({}, this.state.highligtedShape, {this.props.shapeWidth}))
}

// ...

}

You can also take a look of pluggable canvas solutions like react-canvas , but they will bring you farther from the iron and not help you understand what's going on. 您还可以看一下可插拔的画布解决方案,例如react-canvas ,但它们会使您走得更远,无法帮助您了解正在发生的事情。

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

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