无法在同构应用程序中访问React操作

时间:2016-10-10 06:30:27

标签: javascript reactjs isomorphic-javascript alt.js

Iam使用Alt.js和React并在HomeActions.js文件HomeActions.js: Uncaught TypeError: Cannot read property 'updateUserSuccess' of undefined中收到错误。更不用说在完全重新安装node_modules之前,相同的代码完美运行。

这是代码

import alt from '../../alt';
import request from 'superagent';

class HomeActions {
    constructor() {
        this.generateActions(
            'updateUserSuccess',            
        );
    }

    getUser() {
        request
            .get('api/getuser')
            .end((err, res) => {
                this.actions.updateUserSuccess(res.text);
            });
    }                       
}

export default alt.createActions(HomeActions);
在Home.js的getUser()函数中调用

componenetdidmount(){}

同样直接从组件调用action属性确实有效,所以我猜问题不在于Alt.js或generateActions:例如

import alt from '../../alt';

class NavbarActions {
  constructor() {
    this.generateActions(
      'updateOnlineUsers'
    );
  }

}

export default alt.createActions(NavbarActions);

//in Navbar.js
componentDidMount() {
    NavbarStore.listen(this.onChange);
    NavbarActions.updateOnlineUsers(somedata);
  }

//this is working**

这个问题可能与我在实例方法中调用函数的最后一个问题有关。

旁注:更新依赖关系后,我还必须进行更改,以便在服务器端react-middle-ware中使用RouterContext而不是RoutingContext,并解决与之相关的问题我从browserHistory导入react-router(在我createBroswerHistory使用'history/createBrowserHistory'之前)

修改1:我将this.actions.updateUserSuccess(res.text);替换为alt.actions.HomeActions.updateUserSuccess(res.text);updateUserSuccess可供访问但仍会收到警告ReferenceError: An action was called but nothing was dispatched

编辑2:我刚刚在新版本中使用console.log(this);来解决问题的核心,而在旧版本中也是日志

旧版本:

AltAction {id: "HomeActions.getTwoCharacters", actions: Object, actionDetails: Object, alt: Alt, dispatched: false}
_dispatch : ()
actionDetails :
Object actions :
Object alt :
Alt dispatched :
false id :
"HomeActions.getTwoCharacters"
__proto__ : Object

新版

Object {GET_USER: "HomeActions.getUser", LOGOUT: "HomeActions.logout", UPDATE_USER_SUCCESS: "HomeActions.updateUserSuccess", UPDATE_USER_FAIL: "HomeActions.updateUserFail", LOGOUT_SUCCESS: "HomeActions.logoutSuccess"…}
GET_USER : "HomeActions.getUser"
LOGOUT : "HomeActions.logout"
LOGOUT_FAIL : "HomeActions.logoutFail"
LOGOUT_SUCCESS : "HomeActions.logoutSuccess"
UPDATE_USER_FAIL : "HomeActions.updateUserFail"
UPDATE_USER_SUCCESS : "HomeActions.updateUserSuccess"
getUser  :  action()
logout :  action()
logoutFail : action()
logoutSuccess : action()
updateUserFail : action()
updateUserSuccess : action()
__proto__ :  Object

显然,较新版本中没有调度方法。有人可以解释为什么会这样吗?

0 个答案:

没有答案