2016-04-15 24 views
14

Im przy użyciu axios w mojej akcji. Muszę wiedzieć, czy to jest właściwy sposób, aby to zrobić, czy nie.Jak korzystać z Aksios/AJAX z redux-thunk

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; 
} 

a następnie przyjść łącząc je wewnątrz rootReducer.js ==>

import { combineReducers } from 'redux'; 
import reducerWeather from './reducers/reducer_weather'; 

export default combineReducers({ 
    reducerWeather 
}); 

I wreszcie powołanie to wewnątrz mojego pojemnika React som E js plik ...

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); 
+0

Wydaje się to być dobre, jeśli użyjesz razem z nim redux-promise-middleware. – Mozak

Odpowiedz

21

Chyba nie należy (a przynajmniej nie powinien) umieścić obietnicę bezpośrednio w sklepie:

export function FetchWeather(city) { 
    let url = `${API_URL}&q=${city},in`; 
    let promise = axios.get(url); 

    return { 
    type: types.FETCH_WEATHER, 
    payload: promise 
    }; 
} 

ten sposób nie są nawet przy użyciu Redux -thunk, ponieważ zwraca zwykły obiekt. Właściwie Redux-thunk, pozwala na powrót do funkcji, która będzie oceniana później, na przykład coś takiego:

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 
     }).error((response) => dispatch({ 
     type: types.FETCH_WEATHER_FAILURE, 
     error: response.error 
     }) 
    } 
} 

Pamiętaj, aby poprawnie skonfigurować middleware Redux-Thunk. I Naprawdę polecam czytanie redux-thunk documentation i this amazing article, aby mieć głębsze zrozumienie.

+1

Wymyśliłem odpowiedź, opublikuję kod po dotarciu do biura. Jest podobny – STEEL

+0

potrzebujesz serwera do wywołania AJAX, https://github.com/steelx/ReduxWeatherApp/tree/master/server – STEEL