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为什么需要中间件来处理异步数据流? #50

Open
taikulawo opened this issue Oct 11, 2020 · 0 comments
Open

Redux为什么需要中间件来处理异步数据流? #50

taikulawo opened this issue Oct 11, 2020 · 0 comments
Labels

Comments

@taikulawo
Copy link
Collaborator

taikulawo commented Oct 11, 2020

我们明明可以在async之后直接调用dispatch,为什么又要多此一举引入中间件呢?

为什么需要中间件来处理Redux异步数据流?

如果异步之后直接Dispatch

// action creator
function loadData(dispatch, userId) { // needs to dispatch, so it is first argument
  return fetch(`http://data.com/${userId}`)
    .then(res => res.json())f
    .then(
      data => dispatch({ type: 'LOAD_DATA_SUCCESS', data }),
      err => dispatch({ type: 'LOAD_DATA_FAILURE', err })
    );
}

// component
componentWillMount() {
  loadData(this.props.dispatch, this.props.userId); // don't forget to pass dispatch
}

如果使用 react-thunk

// action creator
function loadData(userId) {
  return dispatch => fetch(`http://data.com/${userId}`) // Redux Thunk handles these
    .then(res => res.json())
    .then(
      data => dispatch({ type: 'LOAD_DATA_SUCCESS', data }),
      err => dispatch({ type: 'LOAD_DATA_FAILURE', err })
    );
}

// component
componentWillMount() {
  this.props.dispatch(loadData(this.props.userId)); // dispatch like you usually do
}

可以看到,thunk 只是更改了你的写法。因为不使用 thunk 你需要每次传递给异步函数 dispatch,使用 thunk 后在组件中就可以调用 dispatch,组件不再需要关注 dispatch 派发的函数是不是异步,组件只是发出一个请求而已。组件connect的数据通过 dispatch => reducer 流程最后会触发更新。

Redux-thunk出现是为了解决什么问题?

优雅地进行异步调用,但thunk并不能终止请求,比如component unmounted 之后API调用结束这时会产生副作用,容易导致内存泄漏。
react saga就是为解决上述问题而生的,当然,后面再说 :)

How to dispatch a Redux action with a timeout?

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