如何从Actions中的redux获取数据?

时间:2018-05-13 11:11:14

标签: react-native redux

我试图了解是否有办法在Action中调用redux中的数据。

这是我的行动,我想使用存储在state.auth.user_id和state.auth.token中的user_id和token

jobsActions.js

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/davidgeismar/code/davidgeismar/react-app/nytimes-api/api.py", line 62, in associate_author_article
    print(article)
  File "/Users/davidgeismar/code/davidgeismar/react-app/nytimes-api/models/article.py", line 39, in __repr__
    self.publication_date
IndexError: tuple index out of range

authAction.js

export function createJob(title, company, avatar, shortDescription, description, address, postcode, city, jobType, payment, price, duration, postDate ) {
  return function (dispatch) {
      return axios.post(JOBS_URL(user_id), { title, company, avatar, shortDescription, description, address, postcode, city, jobType, payment, price, duration, postDate }, {
        headers: { authorization: token }
      }).then((response) => {
        dispatch(addJob(response.data.job));
        // console.log(response.data.job)
      }).catch((err) => {
        // console.log(err);
        dispatch(addAlert("Couldn't create job."));
      });
  };
}

2 个答案:

答案 0 :(得分:0)

是。您正在使用redux-thunk,并且thunk函数已经将getState作为第二个参数。只需将您的功能更改为:

export function createJob(title, company, avatar, shortDescription, description, address, postcode, city, jobType, payment, price, duration, postDate ) {
  return function (dispatch, getState) {
      const state = getState();
      const {user_id, token} = state.auth;

      return axios.post(JOBS_URL(user_id), { title, company, avatar, shortDescription, description, address, postcode, city, jobType, payment, price, duration, postDate }, {
        headers: { authorization: token }
      }).then((response) => {
        dispatch(addJob(response.data.job));
        // console.log(response.data.job)
      }).catch((err) => {
        // console.log(err);
        dispatch(addAlert("Couldn't create job."));
      });
  };
}

答案 1 :(得分:0)

之前我遇到过同样的问题并创建了一个增强器来帮助处理它:

redux-named-reducers

它允许从代码中的任何位置进行状态访问,并将reducer状态相互链接。例如,使用enchancer,您的作业减少器可以有一个只读的外部状态,称为令牌,链接到您的auth reducer的令牌:

linkState(jobReducer.token, authReducer.token)

稍后您可以通过操作

访问令牌
createJob() {
   ...
   var token = getState(jobReducer.token)
}

这样做的好处是你的工作模块并不关心令牌的来源。您可以轻松地从不同模块交换身份验证方法:

swith(loginType) {
    case USERNAME_AND_PASSWORD:
       linkState(jobReducer.token, authReducer.token)
    break;
    case FACEBOOK_LOGIN:
       linkState(jobReducer.token, facebookReducer.token)
    break;
    case OAUTH:
       linkState(jobReducer.token, oauthReducer.token)
    break
}