简体   繁体   English

Redux getState在运行中不起作用

[英]Redux getState in action not working

I'm getting a bit confused with getState() in redux. 我对redux中的getState()有点困惑。 I am using the thunk middleware. 我正在使用笨拙的中间件。

I have an auth action which is an async action. 我有一个auth动作,这是一个异步动作。 But I have an action which runs before which checks if a token exists in state and if its still valid. 但是我有一个运行的操作,在此之前检查令牌是否处于状态以及其是否仍然有效。

My problem is i can't seem to check the state when I have called the action. 我的问题是调用动作后,我似乎无法检查状态。 Thought I could just use getState but that doesn't seem to be a function. 以为我可以只使用getState,但这似乎不是一个函数。

container.js container.js

componentDidMount() {
  this.props.authCheck()
}
...
function mapDispatchToProps(dispatch) {
  return {
    authCheck: () => checkApiStatus()(dispatch)
  }
}

Action.js Action.js

export const checkApiStatus = () => (dispatch, getState) => {
  const expires_at = getState().api.expires_at
  if (!expires_at || isDateGreater(expires_at)) {
    // dispatch async action
    dispatch(apiAuth())
  }
  return
}

Anyone have any ideas. 有人有想法么。 Or perhaps better way of implementing something like this? 还是实现这样的更好的方法?

Thanks 谢谢

The problem is you explicitly calling the returned function in you mapDispatchToProps method and passing only one argument. 问题是您在mapDispatchToProps方法中显式调用了返回的函数,并且仅传递了一个参数。 Instead call dispatch(checkApiStatus()) then redux-thunk will take care of passing the right arguments to the returned method. 而是调用dispatch(checkApiStatus())然后redux-thunk将负责将正确的参数传递给返回的方法。 Should look like this 应该看起来像这样

function mapDispatchToProps(dispatch) {
  return {
    authCheck: () => dispatch(checkApiStatus())
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM