Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux-thunk处理异步Action #42

Open
incuisting opened this issue Oct 22, 2017 · 0 comments
Open

Redux-thunk处理异步Action #42

incuisting opened this issue Oct 22, 2017 · 0 comments
Labels

Comments

@incuisting
Copy link
Owner

首先reudx-thunk是一个redux 的中间件,中间件主要完成action在dispatch之后达到reducer之前这个间隙里的操作。

什么是异步action对象?

异步action对象不是对象,它是一个函数,如果不存在redux-thunk,这些函数类型的action会被派发给reducer,然后reducer实际上处理不了这样的函数类型的action,所以就啥屁事没有。
但是!只要有了redux-thunk,它会在dispatch到reducer 的路上对检查action对象是不是函数,如果不是函数类型的action,也啥事不做。如果检查出来是函数类型的action,那么就让它站住,然后执行这个函数类型的action,并把Store的dispatch函数和getState函数作为参数让它吃下去。。到目前为止 这个函数类型的action还是卡在redux-thunk这里,不会继续往前到达reducer。

举个栗子

const increment = () =>({
 type:ActionTypes.INCREMENT
})

const incrementAsync = () =>{ 
 return (dispatch) => { //把Store 的dispatch函数丢进去
   setTimeout (()=> { //定时
      dispatch(increment()); //dispatch 上面那个increment这个Action生成函数
  },1000)
 }
}

异步操作的模式

一般定义一个异步action,要涉及到它的三个状态

  • 异步操作正在进行中
  • 异步操作已经成功完成
  • 异步操作失败
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant