【问题标题】:How can I get the headers from a redux-query response?如何从 redux-query 响应中获取标头?
【发布时间】:2019-08-17 04:21:53
【问题描述】:

我正在发送一个查询请求,并从服务器得到响应就好了。响应标头包含一些我想阅读的信息。我可以看到错误选择器中有 responseHeaders,但我不知道如何获取成功案例的标题。

示例取自https://amplitude.github.io/redux-query/docs/connect-request

import { connect } from 'react-redux';
import { querySelectors } from 'redux-query';
import { connectRequest } from 'redux-query-react';

const getQueries = state => state.queries;

const getNotification = (state, notificationId) => {
  return (state.entities.notificationsById || {})[notificationId];
};

class NotificationView extends Component {
  // ...
}

const mapStateToProps = (state, props) => ({
  isLoading: mapPropsToConfigs(props).some(queryConfig =>
    querySelectors.isPending(getQueries, queryConfig),
  ),
  notification: getNotification(state, props.notificationId),
});

const mapPropsToConfigs = props => [
  {
    url: `/api/notification/${props.notificationId}`,
    transform: responseBody => {
      const { notification } = responseBody.data;

      return {
        notificationsById: {
          [notification.id]: notification,
        },
      };
    },
    update: {
      notificationsById: (prevValue, newValue) => ({
        ...prevCharts,
        ...newValue,
      }),
    },
  },
];

const NotificationViewContainer = compose(
  connect(mapStateToProps),
  connectRequest(mapPropsToConfigs),
)(NotificationView);

在上面的代码中,假设对 /api/notification/${props.notificationId} 的调用返回一个带有名为“MyTag”的标头标签的响应,例如“MyTag: X123”。

我希望能够在 mapStateToProps 的某处读取它。有任何想法吗? 谢谢!

【问题讨论】:

    标签: reactjs react-redux


    【解决方案1】:

    想通了。显然 QuerySelectors 有一个函数 headers 没有记录在 https://amplitude.github.io/redux-query/docs/redux-selectors 中。

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2020-08-10
      • 2017-07-19
      • 2014-03-10
      • 1970-01-01
      • 2017-11-06
      相关资源
      最近更新 更多