想象一下以下组件,它使用相同的变量两次调用相同的函数getHistory()。执行以下代码时,我注意到请求仅执行了一次(并成功执行)。 为什么getHistory()只执行一次?
组件
import React, { Component } from 'react';
import rest from '../redux/reducers/rest';
import { connect } from 'react-redux';
import { render } from 'react-dom';
import { VictoryChart, VictoryLine, VictoryVoronoiContainer } from 'victory';
class HistoryGraph extends Component {
constructor(props){
super(props);
}
componentWillMount(){
this.props.getHistory('a1', 'b1');
this.props.getHistory('a2', 'b2');
}
render() {
return (<div>a</div>)
}
}
const mapDispatchToProps = dispatch => ({
getHistory(a, b) {
dispatch(rest.actions.history({a, b})).catch(err => console.log(err));
}
});
const mapStateToProps = state => ({
history: state.history
});
export default connect(mapStateToProps, mapDispatchToProps)(HistoryGraph);
休息文件(使用redux-api)
import reduxApi, {transformers} from "redux-api";
import adapterFetch from 'redux-api/lib/adapters/fetch';
import map from "lodash/map";
let apiRoot = 'https://pseudo.com/api';
const rest = reduxApi({
history: {
url: `${apiRoot}/history/:a/?anothervariable=:b`,
crud: true,
options: {
method: 'GET'
}
}
}).use('options', (url, params, getState) => {
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
return {headers};
}).use('fetch', adapterFetch(fetch)).use('responseHandler', err => {
if (err) {
console.log(err)
}
});
export default rest;
export const reducers = rest.reducers;