我目前正在尝试从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
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;
}容器/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);操作/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
};
}发布于 2018-06-05 13:28:01
你用来创建redux商店的代码在我看来相当奇怪。如何设置商店:
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(...)的实现是什么
https://stackoverflow.com/questions/50692406
复制相似问题