程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Redux:使用异步中间件与成功功能上的调度动作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Redux:使用异步中间件与成功功能上的调度动作?

开发过程中遇到Redux:使用异步中间件与成功功能上的调度动作的问题如何解决?下面主要结合日常开发的经验,给出你关于Redux:使用异步中间件与成功功能上的调度动作的解决方法建议,希望对你解决Redux:使用异步中间件与成功功能上的调度动作有所启发或帮助;

我个人更喜欢使用自定义中间件来完成此任务。它使操作更容易遵循,并减少了IMO的样板。

我已经设置了中间件来查找从匹配特定签名的操作返回的对象。如果找到此对象架构,则将对其进行特殊处理。

例如,我使用一个看起来像这样的动作:

export function fetchData() {
  return {
    types: [ FETCH_DATA, FETCH_DATA_succesS, FETCH_DATA_FAILURE ],
    promise: API => API('foo/bar')
  }
}

我的自定义中间件看到对象具有types数组和promise函数,并对其进行了特殊处理。看起来是这样的

import 'whatwg-fetch';

function isrequest({ promise }) {
  return promise && typeof promise === 'function';
}

function checkStatus(responsE) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    const error = new Error(response.statusText || response.status);
    error.response = response.Json();
    throw error;
  }
}

function parseJsON(responsE) {
  return response.Json();
}

function makerequest(urlBase, { promise, types, ...rest }, next) {
  const [ requEST, succesS, FAILURE ] = types;

  // dispatch your request action so UI can showing loading inDicator
  next({ ...rest, type: requEST });

  const API = (url, params = {}) => {
    // fetch by default doesn't include the same-origin header.  Add this by default.
    params.credentials = 'same-origin';
    params.method = params.method || 'get';
    params.headers = params.headers || {};
    params.headers['Content-Type'] = 'application/Json';
    params.headers['Access-Control-Allow-Origin'] = '*';

    return fetch(urlBase + url, params)
      .then(checkStatus)
      .then(parseJsON)
      .then(data => {
        // dispatch your success action
        next({ ...rest, payload: data, type: succesS });
      })
      .catch(error => {
        // dispatch your failure action
        next({ ...rest, error, type: FAILURE });
      });
  };

  // Because I'm using promise as a function, I create my own simple wrapper
  // around whatwg-fetch. Note in the action example above, I supply the url
  // and optionally the params and Feed them directly into fetch.

  // The other benefit for this approach is that in my action above, I can do 
  // var result = action.promise(API => API('foo/bar'))
  // result.then(() => { /* something happened */ })
  // This allows me to be notifIEd in my action when a result comes BACk.
  return promise(API);
}

// When setTing up my APIMIDdleware, I pass a base url for the service I am
// using. Then my actions can just pass the route and I append it to the path
export default function APIMIDdleware(urlBasE) {
  return function() {
    return next => action => isrequest(action) ? makerequest(urlBase, action, next) : next(action);
  };
}

我个人喜欢这种方法,因为它集中了很多逻辑,并为您提供了有关API动作的结构的标准实施方式。不利的一面是,对于那些不熟悉redux的人来说,这可能有些神奇。我还使用了thunk中间件,到目前为止,这两者都可以满足我的所有需求。

解决方法

我正在尝试将Redux集成到我的React项目中。目前,我没有使用任何Flux框架。

我的应用程序从API获取一些数据,并以一种漂亮的方式显示它们,如下所示:

componentDidMount() {
  getData();
}

getData() {
  const self = this;

  ajax({
    url: apiUrl,})
  .success(function(data) {
    self.setState({
      data: data,});
  })
  .error(function() {
    throw new Error('Server response failed.');
  });
}

在阅读有关Redux的文章时,我确定了两种可用于处理将成功数据存储在商店中的方法:

  • 使用异步中间件,或
  • ADD_DATA从ajax函数的成功回调中分派动作

但是我不确定哪种方法更好。

回调中的分派操作听起来很容易实现和理解,而异步中间件则很难向不习惯使用功能性语言的人解释。

大佬总结

以上是大佬教程为你收集整理的Redux:使用异步中间件与成功功能上的调度动作全部内容,希望文章能够帮你解决Redux:使用异步中间件与成功功能上的调度动作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: