[英]Alt + React - invalid action reference while following alt tutorial
我正在嘗試在我的React應用程序中實現Alt。 目前我正在檢查它,只是按照教程嘗試了一些東西。 現在我遇到了商店無法識別操作的問題。
這是動作類:
import alt from '../libs/alt';
import WishSource from '../sources/WishSource';
class WishActions {
fetchWishes() {
this.dispatch();
WishSource.fetch()
.then((wishes) => {
this.actions.fetchWishesSuccess(wishes);
})
.catch((err) => {
this.actions.fetchWishesFailed(err);
});
}
fetchWishesSuccess(wishes) {
this.dispatch(wishes);
}
fetchWishesFailed(err) {
this.dispatch(err);
}
}
export default alt.generateActions(WishActions);
我嘗試在我的商店中綁定這些操作,如下所示:
import alt from '../libs/alt';
import WishActions from '../actions/WishActions';
class WishStore {
constructor() {
this.wishes = [];
this.errorMessage = null;
this.bindListeners({
handleFetchWishes: WishActions.FETCH_WISHES,
handleFetchWishesSuccess: WishActions.FETCH_WISHES_SUCCESS,
handleFetchWishesFailed: WishActions.FETCH_WISHES_FAILED
});
}
handleFetchWishesSuccess(wishes) {
this.wishes = wishes;
this.errorMessage = null;
}
handleFetchWishes() {
this.wishes = [];
}
handleFetchWishesFailed(err) {
this.errorMessage = err;
}
}
export default alt.createStore(WishStore, 'WishStore');
我一直在給我錯誤
'傳入的動作參考無效'
問題出在bindListeners函數中。
如果我嘗試bindActions它會說:
'_WishActions2.default.fetchWishes不是函數'
這是在視圖中。 在這里,我在componentDidMount()
調用WishActions.fetchWishes()
componentDidMount()
我無法理解這里出了什么問題。 如果我查看教程和其他示例,這應該可行。
有人可以幫助我在這里,也許可以解釋什么是錯的?
我發現了什么問題,並將留在這里,以防其他人遇到同樣的事情。
問題是我使用generateActions而不是createActions。 您可以對類使用createActions,對簡單函數使用generateActions。
generateActions('create')將創建一個函數create =(x)=> {return x; 並派遣它。 您仍然可以在構造函數中使用this.generateActions在您的類中使用它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.