【问题标题】:Can't resolve axios promise in redux reducer function无法解决 redux reducer 函数中的 axios promise
【发布时间】:2019-06-13 05:23:19
【问题描述】:

我正在尝试从 slack api 获取频道列表并将它们存储在 redux 存储中并做出反应。

axios 得到一个 [[PromiseValue]] ,但我无法获取其中的对象:

reducer 文件:

import * as actionTypes from "../actions/types";
import { getChannels } from "./../services/channelService";

const channelsReducer = (state = getChannels(), action) => {
    switch (action.type) {
        case actionTypes.GET_CHANNELS:
            return state;

        default:
            return state;
    }
};

channelService.js 文件:

import axios from "axios";

export const getChannels = async () => {
    const token =
        "token";
    return await axios
        .get(`https://slack.com/api/conversations.list?token=${token}`)
        .then(response => {
            return response.data;
        });
};

action.js 文件:

import * as actionTypes from "./types";

export const getChannels = () => async dispatch => {
    dispatch({
        type: actionTypes.GET_CHANNELS
    });
};

还有我的组件文件:

import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as actions from "./../actions/channelsActions";

class Navbar extends Component {
    state = {};
    componentDidMount() {}
    render() {
        console.log(this.props.channels);
    }
}

const mapStateToProps = state => {
    return {
        channels: state.channels
    };
};

const mapDispatchToProps = dispatch => {
    return bindActionCreators(actions, dispatch);
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Navbar);

如何从 axios 访问这个 Promise 中的对象?

【问题讨论】:

    标签: javascript reactjs redux promise


    【解决方案1】:

    你不能在你的 reducer 中调用getChannels(),因为它是一个异步函数并且你的 reducer 是同步的。但是,您可以在您的 api 响应返回后调度您的操作

    export const getChannels = (dispatch) => {
        const token = "token";
        axios
            .get(`https://slack.com/api/conversations.list?token=${token}`)
            .then(response => {
                dispatch(//your action here... )
            });
    };
    

    从您的组件中调用它并传入调度。

    您也可以按照 Emile 的建议修改现有操作:

      export const getChannels = () => dispatch => {
         const token = "token";
         axios
            .get(`https://slack.com/api/conversations.list?token=${token}`)
            .then(response => {
                dispatch({
                     type: actionTypes.GET_CHANNELS, 
                     payload: response.data
                   })
            });
        };    
     };
    

    【讨论】:

    • 我不想污染我的组件。我必须这样做吗?
    • 不,如果您使用的是 redux,则不会。你可以在任何你喜欢的地方触发通道获取,并派发一个动作将它弹出到商店中,然后通过connect() 在你的组件中获取它
    • 我现在就在做。在代码中重新检查我的组件部分
    • 太棒了,在这种情况下,您可以随意触发您的 api 调用(但您需要通过调度访问商店,所以在您的 <Provider>
    • @Afshin 做到这一点的方法是在你的行动中,我看到你已经在使用 thunk-actions 所以只需从你的getChannels 函数调用@987654327 @ 文件在您的 getChannels 操作中。因此,您的操作非常简单,就像您现在已经做的那样,将获取逻辑隔离到您的服务文件。
    猜你喜欢
    • 2018-08-09
    • 2018-02-21
    • 2019-08-10
    • 2020-06-02
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    相关资源
    最近更新 更多