【发布时间】:2018-07-03 19:18:08
【问题描述】:
我正在尝试使用 Axios 和 redux 从 API 响应调用返回一些数据。但是,我坚持如何检索数据。日志记录工作得很好,但我似乎无法访问承诺中的数据。任何帮助将不胜感激。
视频播放器.js
import React, { Component } from 'react';
import { connect } from "react-redux";
import * as actions from "../actions";
class Videoplayer extends Component {
renderContent() {
return this.props.fetchSong("house"); // this SHOULD return a URL
}
render() {
return (
<video className="responsive-video" controls autoPlay>
<source src={this.renderContent()} type="video/mp4"></source>
</video>
)
}
}
export default connect(null, actions)(Videoplayer);
index.js(操作文件夹)
import axios from "axios";
import { FETCH_SONG } from "./types";
export const fetchSong = category => async dispatch => {
const res = await axios.get(`/api/${category}`);
dispatch({ type: FETCH_SONG, payload: res.data });
};
genreReducer.js
import { FETCH_SONG } from "../actions/types";
export default function(state = null, action) {
switch (action.type) {
case FETCH_SONG:
console.log(action.payload);
return action.payload;
default:
return state;
}
}
【问题讨论】:
-
我认为这应该没有必要,因为 redux-thunk 处理了这个。
标签: reactjs asynchronous redux axios redux-thunk