JBE*_*JBE 7 javascript ecmascript-6 reactjs traceur
注意:此帖已在React不支持ES6(v12)时发布.
我有一个ES6课程:
class BaseClass {
getInitialState(){
return {message: 'Hello!'};
}
render() {
return (
<div>
<div>{this.state.message}</div>
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用这个表达式在ES6中导出(来源:反应ES6 browserify)
export default React.createClass(BaseClass.prototype)
Run Code Online (Sandbox Code Playgroud)
这很好用.现在我想使用ES6继承来扩展我的BaseClass类:
class ExtendedClass extends BaseClass{
getInitialState(){
return {message: "Hello! I'm an extension"};
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我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.
Run Code Online (Sandbox Code Playgroud)
我知道React 0.13应该更符合ES6,但有没有办法解决这个问题?
编辑:
我正在使用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);
Run Code Online (Sandbox Code Playgroud)
关于在ES6中编写ReactJS的帖子很棒.
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;
Run Code Online (Sandbox Code Playgroud)
欢迎面板组件:
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 }
Run Code Online (Sandbox Code Playgroud)
这是我找到的解决方法:
在库内部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,
...
}
Run Code Online (Sandbox Code Playgroud)
然后在 中ExtendedClass,即使您不自定义每个方法,您也必须重新定义它们:
class ExtendedClass extends BaseClass{
getInitialState(){
return {message: "Hello! I'm an extension"};
}
/** required */
render(){
return super.render();
}
}
Run Code Online (Sandbox Code Playgroud)
我对这个肮脏的解决方案并不满意,但它会完成等待 0.13 版本的工作,希望能够解决这些问题。
| 归档时间: |
|
| 查看次数: |
3504 次 |
| 最近记录: |