ES7装饰器并没有使用" displayName"组

时间:2015-08-18 10:26:29

标签: javascript reactjs decorator ecmascript-7

我将React DnD添加到我正在构建的应用中,并且已经意识到ES7装饰器无法正常工作(据我所知),语法如下:

@DragDropContext(HTML5Backend)
export class App extends React.Component {
    ...
}
App.displayName = 'My App'

然而,如果我删除App.displayName行,一切都还可以。为什么?

我应该如何设置displayName属性?

1 个答案:

答案 0 :(得分:3)

由于您已经在使用ES7功能,因此可以使用ES7 class properties。这些都在第0阶段。

@DragDropContext(HTML5Backend)
export class App extends React.Component {
    static displayName = 'My App';
    //or just
    //displayName = 'My App';
}

如果您使用的是babel,则需要使用

启用这些功能
babel --stage 0

babel --optional es7.classProperties
相关问题