我正在尝试在以下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'.`
(我正在使用打字稿)
有人知道什么可能导致此错误吗?预先感谢!
答案 0 :(得分:0)
我也遇到了同样的问题,因为您正尝试像我一样在ES6中运行Flux,这很可能发生在您身上。
如果是这种情况,那么根据this在其GitHub存储库中的问题,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));