Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 正在将getDefaultProps重构到ES6。。。什么';我的代码怎么了?_Reactjs_Ecmascript 6 - Fatal编程技术网

Reactjs 正在将getDefaultProps重构到ES6。。。什么';我的代码怎么了?

Reactjs 正在将getDefaultProps重构到ES6。。。什么';我的代码怎么了?,reactjs,ecmascript-6,Reactjs,Ecmascript 6,我有以下React.createClass,我想把它传递给扩展React.Component: var Rect = React.createClass({ getDefaultProps: function() { return { width: 0, height: 0, x: 0, y: 0 }

我有以下
React.createClass
,我想把它传递给
扩展React.Component

  var Rect = React.createClass({
        getDefaultProps: function() {
            return {
                width: 0,
                height: 0,
                x: 0,
                y: 0
            }
        },

        render: function() {
            return (
              <rect className="bar"
                    height={this.props.height} 
                    y={this.props.y} 
                    width={this.props.width}
                    x={this.props.x}
              >
              </rect>
            );
        },
    });
var Rect=React.createClass({
getDefaultProps:function(){
返回{
宽度:0,
高度:0,,
x:0,,
y:0
}
},
render:function(){
返回(
);
},
});
我打了一针,但没用:

class Rect extends React.Component {
    constructor(props) {
      super(props);
    }
    render() {
        return (
          <rect className="bar"
                height={this.props.height} 
                y={this.props.y} 
                width={this.props.width}
                x={this.props.x}
          >
          </rect>
        );
    }
  Rect.defaultProps = { 
            width: 0,
            height: 0,
            x: 0,
            y: 0 
    }
};
类Rect扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
返回(
);
}
Rect.defaultProps={
宽度:0,
高度:0,,
x:0,,
y:0
}
};

那怎么了

在这种情况下,
defaultProps
定义需要在类定义之外:

class Rect extends React.Component {
    constructor(props) {
      super(props);
    }
    render() {
        return (
          <rect className="bar"
                height={this.props.height} 
                y={this.props.y} 
                width={this.props.width}
                x={this.props.x}
          >
          </rect>
        );
    }
};
Rect.defaultProps = { 
        width: 0,
        height: 0,
        x: 0,
        y: 0 
}
类Rect扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
返回(
);
}
};
Rect.defaultProps={
宽度:0,
高度:0,,
x:0,,
y:0
}

在这种情况下,
defaultProps
定义需要在类定义之外:

class Rect extends React.Component {
    constructor(props) {
      super(props);
    }
    render() {
        return (
          <rect className="bar"
                height={this.props.height} 
                y={this.props.y} 
                width={this.props.width}
                x={this.props.x}
          >
          </rect>
        );
    }
};
Rect.defaultProps = { 
        width: 0,
        height: 0,
        x: 0,
        y: 0 
}
类Rect扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
返回(
);
}
};
Rect.defaultProps={
宽度:0,
高度:0,,
x:0,,
y:0
}