Min Htet Oo
Min Htet Oo

Reputation: 536

Redux-Promise not working

I'm currently trying to resolve a promise from axios and get data with Redux-Promise Middleware.I'have already registered Redux-Promise middleware.In the search_bar.js, fetchWeather Action Creator is called.It creates the axio promise and passes to the reducers.In reducer_weather.js , the promise isn't still resolved.Can someone help me with this?

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import ReduxPromise from "redux-promise";

import App from "./components/app";
import reducers from "./reducers";

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <App />
  </Provider>,
  document.querySelector(".container")
);

reducers/index.js

import { combineReducers } from "redux";
import WeatherReducer from "./reducer_weather";

const rootReducer = combineReducers({
  weather: WeatherReducer
});

export default rootReducer;

reducers/reducer_weather.js

export default function(state = null, action){
console.log("Reducers");
console.log(action);
return state;
}

containers/search_bar.js

    import React,{Component} from 'react';
    import {connect} from 'react-redux';
    import {bindActionCreators} from 'redux';
    import {fetchWeather} from '../actions/index';

    class SearchBar extends Component {
    constructor(props){
        super(props);

        this.state = {
            term:''
        };

        this.onInputChange = this.onInputChange.bind(this);
        this.onFormSubmit = this.onFormSubmit.bind(this);
    }

    onInputChange(e){
        this.setState({term:e.target.value});
    }

    onFormSubmit(e){
        e.preventDefault();
        this.props.fetchWeather(this.state.term);
        this.setState({term:''})
    }

    render(){
        return (
        <form className="input-group" onSubmit={this.onFormSubmit}>
            <input value={this.state.term} onChange={this.onInputChange}/>
            <span className="input-group-btn">
                <button type="submit" className="btn btn-secondary">submit</button>
            </span>
        </form>
        );
    }
}

function mapDispatchToProps(dispatch){
    return bindActionCreators({fetchWeather},dispatch);
}

export default connect(null,mapDispatchToProps)(SearchBar);

actions/index.js

import { request } from "http";
import axios from 'axios';

const API_KEY = "eba56fc1ee64a90231051880ed9788d6";
const ROOT_URL = `http://api.openweathermap.org/data/2.5/forecast?appid=${API_KEY}`;

export function fetchWeather(city){

    const url = `${ROOT_URL}&q=${city},us`;
    const request = axios.get(url);

    console.log("Actions");
    console.log(request);
    return {
        type: fetchWeather,
        payload: request
    };
}

Upvotes: 0

Views: 720

Answers (1)

sn42
sn42

Reputation: 2444

Your code to create the redux store looks quite odd to me. How I setup the store:

import { createStore, applyMiddleware } from 'redux';
import promiseMiddleware from 'redux-promise';

const store = createStore(
    rootReducer,
    applyMiddleware(promiseMiddleware)
);

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.querySelector(".container")
);

Also, what's the implementation of bindActionCreators(...)?

Upvotes: 1

Related Questions