首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Redux-Promise不工作

Redux-Promise不工作
EN

Stack Overflow用户
提问于 2018-06-05 13:05:22
回答 1查看 615关注 0票数 0

我目前正在尝试从axio解析promise并使用Redux-Promise中间件获取数据。我已经注册了Redux-Promise middleware.In search_bar.js,fetchWeather操作创建者是called.It创建axio promise并传递给reducers.In reducer_weather.js,promise不是仍然resolved.Can有人帮我做这个吗?

index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
import { combineReducers } from "redux";
import WeatherReducer from "./reducer_weather";

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

export default rootReducer;

reducers/reducer_weather.js

代码语言:javascript
复制
export default function(state = null, action){
console.log("Reducers");
console.log(action);
return state;
}

容器/search_bar.js

代码语言:javascript
复制
    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);

操作/index.js

代码语言:javascript
复制
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
    };
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-05 13:28:01

你用来创建redux商店的代码在我看来相当奇怪。如何设置商店:

代码语言:javascript
复制
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(...)的实现是什么

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50692406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档