Flux Utils TypeError:如果没有'new',则不能调用类构造函数App

问题描述 投票:9回答:1

我正在尝试在以下React组件上实现Flux Util容器:

class App extends React.Component<{},AppState> {
 constructor(props:Readonly<{}>){
    super(props);   
 }
 static getStores(){
    return [ArticlesStore];
 }

 static calculateState(prevState:AppState):AppState{
    return {
        articles:ArticlesStore.getState()
    }
 }
 render() {
    return (
        <main>
            <Navbar></Navbar>
            <Routes></Routes>
        </main>
    );
 }
}


interface AppState{
 /**
  * Articles retrived from the ArticlesState to be used in the rendering of the page
  */
 articles:ArticlesStoreState;
}

export default Container.create(App);

在实现创建容器所需的代码时,我遵循了flux网站上提供的示例以及在GitHub上找到的其他一些代码作为参考。但是在运行此代码时,出现以下错误:

`TypeError: Class constructor App cannot be invoked without 'new'.` 

((我正在使用打字稿)

没有人知道什么可能导致此错误吗?预先感谢!

reactjs typescript flux reactjs-flux
1个回答
0
投票

我也遇到了同样的问题,因为您正试图像我一样在ES6中运行Flux,这很可能发生在您身上。

如果是这种情况,那么根据其GitHub存储库中的this问题,Flux容器仍不支持ES6。

在注释中看到的可能的(丑陋的)解决方法是执行以下操作:

/// FluxContainerConverter.js

module.exports = {
    convert: function(containerClass) {
        const tmp = containerClass;
        containerClass = function(...args) {
            return new tmp(...args);
        };
        containerClass.prototype = tmp.prototype;
        containerClass.getStores = tmp.getStores;
        containerClass.calculateState = tmp.calculateState;
        return containerClass;
    }
};

现在您可以像这样使用它来创建您的FluxContainer:

var fluxContainerConverter = require('./FluxContainerConverter');

Container.create(
    fluxContainerConverter.convert(MyComponent));
© www.soinside.com 2019 - 2024. All rights reserved.