【发布时间】:2018-05-26 01:52:07
【问题描述】:
我正在尝试使用 Giphy API、redux 和 axios 制作一个搜索应用程序,但我认为我在请求从 API 获取所有搜索结果时出错了。
我使用一个动作发出 API 请求,该动作被减速器捕获,但是当我 console.log 记录减速器内的动作值时,我得到的是 [object Object] 而不是实际的对象。为什么是这样?
我使用 ReduxPromise 作为我的中间件。
这是我在操作代码中的 API 请求:
import axios from 'axios';
export const FETCH_GIPHS = 'FETCH_GIPHS'
export function fetchGiphs(value) {
const api = "http://api.giphy.com/v1/gifs/search";
const API_KEY = 'hdUk5buTTISSC29bx2DAXfDRCz6tkcrS';
const url = `${api}?q=${value}&api_key=${API_KEY}&limit=5"`;
//http://api.giphy.com/v1/gifs/search?q=rainbow&api_key=hdUk5buTTISSC29bx2DAXfDRCz6tkcrS&limit=5"
const request = axios.get(url);
console.log('Request:', request)
return {
type: FETCH_GIPHS,
payload: request
}
}
和减速器:
export default function(state = null, action) {
console.log('action recieved: ' + action)
return state;
}
还有我的主要 index.js,我的中间件在哪里
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.getElementById('root'));
【问题讨论】:
-
因为
const request = axios.get(url)将是异步调用,而您将在此之前返回操作。你需要检查How to return response from async call -
但我使用的是 redux 承诺,所以这不意味着我会在减速器中返回对象吗?但是,当我在控制台记录操作时,我只会得到 [object Object] 而不是实际的对象...
标签: reactjs redux react-redux axios redux-promise