如何在 redux-thunk 中使用 axios / AJAX
How to use axios / AJAX with redux-thunk
我在我的操作中使用 axios。我需要知道这样做是否正确。
actions/index.js
==>
import axios from 'axios';
import types from './actionTypes'
const APY_KEY = '2925805fa0bcb3f3df21bb0451f0358f';
const API_URL = `http://api.openweathermap.org/data/2.5/forecast?appid=${APY_KEY}`;
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
let promise = axios.get(url);
return {
type: types.FETCH_WEATHER,
payload: promise
};
}
reducer_weather.js
==>
import actionTypes from '../actions/actionTypes'
export default function ReducerWeather (state = null, action = null) {
console.log('ReducerWeather ', action, new Date(Date.now()));
switch (action.type) {
case actionTypes.FETCH_WEATHER:
return action.payload;
}
return state;
}
然后在里面组合它们 rootReducer.js ==>
import { combineReducers } from 'redux';
import reducerWeather from './reducers/reducer_weather';
export default combineReducers({
reducerWeather
});
最后在我的 React 容器中调用它一些 js 文件...
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {FetchWeather} from '../redux/actions';
class SearchBar extends Component {
...
return (
<div>
...
</div>
);
}
function mapDispatchToProps(dispatch) {
//Whenever FetchWeather is called the result will be passed
//to all reducers
return bindActionCreators({fetchWeather: FetchWeather}, dispatch);
}
export default connect(null, mapDispatchToProps)(SearchBar);
我猜你不应该(或者至少不应该)将承诺直接放在商店里:
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
let promise = axios.get(url);
return {
type: types.FETCH_WEATHER,
payload: promise
};
}
这样您甚至不用使用 redux-thunk,因为它 return 是一个普通对象。实际上,redux-thunk 使您能够 return 稍后将评估的函数,例如,如下所示:
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
return function (dispatch) {
axios.get(url)
.then((response) => dispatch({
type: types.FETCH_WEATHER_SUCCESS,
data: response.data
})).catch((response) => dispatch({
type: types.FETCH_WEATHER_FAILURE,
error: response.error
}))
}
}
确保正确设置 redux-thunk 中间件。我非常推荐阅读redux-thunk documentation and this amazing article以加深理解。
我在我的操作中使用 axios。我需要知道这样做是否正确。
actions/index.js
==>
import axios from 'axios';
import types from './actionTypes'
const APY_KEY = '2925805fa0bcb3f3df21bb0451f0358f';
const API_URL = `http://api.openweathermap.org/data/2.5/forecast?appid=${APY_KEY}`;
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
let promise = axios.get(url);
return {
type: types.FETCH_WEATHER,
payload: promise
};
}
reducer_weather.js
==>
import actionTypes from '../actions/actionTypes'
export default function ReducerWeather (state = null, action = null) {
console.log('ReducerWeather ', action, new Date(Date.now()));
switch (action.type) {
case actionTypes.FETCH_WEATHER:
return action.payload;
}
return state;
}
然后在里面组合它们 rootReducer.js ==>
import { combineReducers } from 'redux';
import reducerWeather from './reducers/reducer_weather';
export default combineReducers({
reducerWeather
});
最后在我的 React 容器中调用它一些 js 文件...
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {FetchWeather} from '../redux/actions';
class SearchBar extends Component {
...
return (
<div>
...
</div>
);
}
function mapDispatchToProps(dispatch) {
//Whenever FetchWeather is called the result will be passed
//to all reducers
return bindActionCreators({fetchWeather: FetchWeather}, dispatch);
}
export default connect(null, mapDispatchToProps)(SearchBar);
我猜你不应该(或者至少不应该)将承诺直接放在商店里:
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
let promise = axios.get(url);
return {
type: types.FETCH_WEATHER,
payload: promise
};
}
这样您甚至不用使用 redux-thunk,因为它 return 是一个普通对象。实际上,redux-thunk 使您能够 return 稍后将评估的函数,例如,如下所示:
export function FetchWeather(city) {
let url = `${API_URL}&q=${city},in`;
return function (dispatch) {
axios.get(url)
.then((response) => dispatch({
type: types.FETCH_WEATHER_SUCCESS,
data: response.data
})).catch((response) => dispatch({
type: types.FETCH_WEATHER_FAILURE,
error: response.error
}))
}
}
确保正确设置 redux-thunk 中间件。我非常推荐阅读redux-thunk documentation and this amazing article以加深理解。