注:,这篇文章在当时已经发布了,React并不支持ES6 (v12)。
我有一个ES6类:
class BaseClass {
getInitialState(){
return {message: 'Hello!'};
}
render() {
return (
<div>
<div>{this.state.message}</div>
</div>
)
}
}我可以使用这个表达式在ES6中导出(源:反应ES6褐化)
export default React.createClass(BaseClass.prototype)这个很好用。现在我想使用ES6继承来扩展我的BaseClass类:
class ExtendedClass extends BaseClass{
getInitialState(){
return {message: "Hello! I'm an extension"};
}
}但是,当我在React.createClass类上调用ExtendedClass时,我得到了以下异常:
Invariant Violation: ReactCompositeComponentInterface: You are attempting to define `constructor` on your component more than once. This conflict may be due to a mixin.我知道React 0.13应该更友好一些,但是有什么方法可以解决这个问题吗?
编辑:
我正在使用Traceur编译我的ES6类。ExtendedClass的输出如下所示:
function ExtendedClass() {
"use strict";
if (BaseClass !== null) {
BaseClass.apply(this, arguments);
}
}
for (BaseClass____Key in BaseClass) {
if (BaseClass.hasOwnProperty(BaseClass____Key)) {
ExtendedClass[BaseClass____Key] = BaseClass[BaseClass____Key];
}
}
____SuperProtoOfBaseClass = BaseClass === null ? null : BaseClass.prototype;
ExtendedClass.prototype = Object.create(____SuperProtoOfBaseClass);
ExtendedClass.prototype.constructor = ExtendedClass;
ExtendedClass.__superConstructor__ = BaseClass;
ExtendedClass.prototype.getInitialState = function() {
"use strict";
return {message: "Hello! I'm an extension"};
};
React.createClass(ExtendedClass.prototype);发布于 2015-07-26 08:38:35
关于用ReactJS编写ES6,有一篇很棒的文章。
http://ilikekillnerds.com/2015/02/developing-react-js-components-using-es6/
无论如何,在使用ES6时,您不使用React.createClass。您只需创建扩展React.Component的类
同样在ES6中,您没有getInitialState而不是defaultProps。取而代之的是在构造函数中使用this.state。
看看这个例子。假设您有Card组件和扩展Card组件的欢迎面板。
守则如下:
卡组件:
import React , { Component } from 'react'
class Card extends Component {
constructor(props){
super(props);
}
render() {
return (
<div className="card">
<div className="card__content">
<label>{this.props.content}</label>
</div>
</div>
)
}
initPanel(el,content){
React.render( <Card content={content} />, el);
}
}
export default Card;欢迎面板组件:
import React from 'react';
import Card from 'components/card.component';
class WelcomePanel extends Card {
constructor(props){
super(props);
this.state = {
content: "Welcome Panel",
index: 0
}
}
componentClicked(){
this.setState({content: "Component Clicked", index: this.state.index + 1})
}
render() {
return (
<div className="welcome-panel" onClick={this.componentClicked.bind(this)}>
<Card content={`${this.state.content} ${this.state.index > 0 ? this.state.index : ''} ${this.state.index > 0 ? 'times' : ''} `} />
</div>
)
}
initPanel(el,content){
React.render( <WelcomePanel />, el);
}
}
export default { Class: WelcomePanel }发布于 2014-12-01 20:29:53
下面是我找到的解决办法:
在React.js库中,我更新了ReactCompositeComponentInterface以添加constructor的自定义策略(据我所知,无法正确自定义这个“接口”):
var ReactCompositeComponentInterface = {
/**
* An array of Mixin objects to include when defining your component.
*
* @type {array}
* @optional
*/
mixins: SpecPolicy.DEFINE_MANY,
/**
* Custom policy for 'constructor'
*/
constructor: SpecPolicy.DEFINE_MANY,
...
}然后,在ExtendedClass中,您必须重新定义每个方法,即使您没有自定义它们:
class ExtendedClass extends BaseClass{
getInitialState(){
return {message: "Hello! I'm an extension"};
}
/** required */
render(){
return super.render();
}
}我不满意这个肮脏的解决方案,但它将完成工作,等待一个0.13版本,希望能解决这些问题。
https://stackoverflow.com/questions/27233491
复制相似问题