我正在尝试使用Axios和redux从API响应调用返回一些数据。然而,我被困在如何检索数据的问题上。日志工作得很好,但我似乎无法访问promise中的数据。任何帮助都将不胜感激。
Videoplayer.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 (actions文件夹)
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;
}
}
https://stackoverflow.com/questions/51161590
复制相似问题