Redux-Promise не работает - PullRequest
0 голосов
/ 05 июня 2018

В настоящее время я пытаюсь разрешить обещание из axios и получить данные с помощью промежуточного программного обеспечения Redux-Promise. Я уже зарегистрировал промежуточное программное обеспечение Redux-Promise. В search_bar.js вызывается fetchWeather Action Creator. Он создает обещание axio.и переходит к редукторам. В reducer_weather.js обещание все еще не выполнено. Может ли кто-нибудь помочь мне с этим?

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")
);

redurs / index.js

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

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

export default rootReducer;

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

1 Ответ

0 голосов
/ 05 июня 2018

Ваш код для создания магазина приставок выглядит довольно странно для меня.Как настроить магазин:

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")
);

Также, какова реализация bindActionCreators(...)?

...