所以,我想使用另一个名为UserStore
的商店的存储数据。操作为FavoriteActions
,FavoriteStore
。如果我在UserStore.getState()
上的某个方法中调用FavoriteActions
,则会显示UserStore.getState is not a function
。很奇怪,我有一个使用UserStore
的类似代码,它可以工作。我已经在文件的顶部要求它了。
这是文件。
var alt = require('../utils/alt');
var UserStore = require('../stores/UserStore');
var FavoriteActions = alt.createActions({
favorite: function(variant_id, list_id) {
this.dispatch();
# THIS IS THE PROBLEM
var token = UserStore.getState().current_user.token;
var headers = { 'X-Spree-Token': token };
var params = {
variant_id: variant_id
};
axios.get(base_url + '/shopping_lists/' + list_id + '/items', {
headers: headers,
params: params
})
.then(FavoriteActions.updateFavorite)
.catch(FavoriteActions.favoritesFailed);
},
updateFavorite: function(result) {
this.dispatch(result);
},
updateFavoriteId: function(list_id) {
this.dispatch(list_id);
},
updateFavorites: function(result) {
this.dispatch(result);
},
favoritesFailed: function(error_message) {
this.dispatch(error_message);
},
});
module.exports = FavoriteActions;
我正在使用alt。这是UserStore
文件。
var alt = require('../utils/alt');
var UserActions = require('../actions/UserActions');
var UserStore = alt.createStore({
displayName: 'UserStore',
bindListeners: {
login: UserActions.LOGIN,
logout: UserActions.LOGOUT,
updateUser: UserActions.UPDATE_USER,
clearUser: UserActions.CLEAR_USER,
loginFailed: UserActions.LOGIN_FAILED
},
state: {
current_user: null,
errorMessage: null,
loading: false
},
login: function() {
this.setState({
loading: true
});
},
updateUser: function(user) {
this.setState({
current_user: user,
loading: false
});
},
logout: function() {
this.setState({
loading: true
});
},
clearUser: function() {
this.setState({
current_user: null,
loading: false
});
// FIXME: Unset user session
},
loginFailed: function(errorMessage) {
this.setState({
errorMessage: errorMessage,
loading: false
});
}
});
module.exports = UserStore;