如何从redux-query响应中获取标头?

时间:2019-08-17 04:21:53

标签: reactjs react-redux

我正在发送查询请求,并从服务器获得响应就可以了。响应头包含一些我想阅读的信息。我可以看到错误选择器中有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中的某处读取它。有任何想法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

弄清楚了。显然,QuerySelectors具有https://amplitude.github.io/redux-query/docs/redux-selectors中未记录的功能headers