我正在尝试使用我创建的API,我已经遵循了之前使用的代码,但是当加载组件时,由于json列表为空,它显示并清空列表,我可以在记录以后加载列表但没有刷新或组件上的任何内容。我试图添加一个验证,如果列表的长度是cero只是不打印任何东西,但这会导致错误。我猜可能有关于中间件的问题(我正在使用redux-promise)。正如您所看到的,我已经在应用程序定义中添加了中间件,我无法看到它缺少什么想法?这是我的代码:
动作/ index.js:
import { FETCH_TESTS } from '../actions/index';
export default function(state = [], action){
switch (action.type) {
case FETCH_TESTS:
return [action.payload.data, ...state]; //ES6 syntaxis\
}
return state;
}
减速器/ reducer_tests.js
import { combineReducers } from 'redux';
import TestsReducer from './reducer_tests';
const rootReducer = combineReducers({
tests: TestsReducer
});
export default rootReducer;
动作/ index.js
import _ from 'lodash';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchTests } from '../actions';
class TestList extends Component{
componentDidMount(){
this.props.fetchTests();
}
renderTest(){
return _.map(this.props.tests, test => {
return (
<tr key={test.id}>
<td>{test.id}</td>
<td>{test.col1}</td>
<td>{test.col2}</td>
<td>{test.col3}</td>
<td>{test.col4}</td>
</tr>
);
});
}
render(){
return (
<table className="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
{ this.renderTest() }
</tbody>
</table>
);
}
}
function mapStateToProps(state){
return {tests: state.tests}
}
//export default connect(mapStateToProps)(TestList)
export default connect(mapStateToProps, { fetchTests})(TestList);
容器/ list_tests.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.getElementById('root'));
index.js
{
"name": "someapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0",
"redux-logger": "^3.0.6",
"redux-promise": "^0.5.3",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
的package.json
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
data: Array [ {…} ]
headers: Object { "content-type": "application/json" }
request: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }
编辑: 从动作创建者(该数组包含api列表入口点上的唯一对象):
payload:[object Object]
状态:200 statusText:&#34; OK&#34; proto :对象{...}
来自reducer的:
{{1}}
如果我在容器上记录测试道具,首先记录一个空数组[]然后记录一个长度为1的数组
答案 0 :(得分:-1)
您必须致电调度员更新商店。
在containers/list_tests.js
const mapDispatchToProp = dispatch => ({
fetchTests : () => dispatch(fetchTests())
})
export default connect(mapStateToProps, mapDispatchToProp )(TestList);
export function fetchTests(){
return axios.get(`${ROOT_URL}/tests/?format=json`)
.then(res => {
return {
type: FETCH_TESTS,
payload: res.data
}
});
}
答案 1 :(得分:-1)
const request = axios.get(`${ROOT_URL}/tests/?format=json`);
// here is your issue because axios returns promise.
像这样创建一个doGet方法。
export function doGet(url, onSuccess, onFailure) {
return axios.get(url)
.then((response) => {
if (onSuccess) {
onSuccess(response);
}
return response.data || {};
})
.catch((error) => {
if (onFailure) {
onFailure(error);
}
});
}
并在componentDidMount
上调用此方法,例如:
doGet(
'your/api/url',
(response) => console.log('Api success callback', response),
(error) => console.log('error callback', error)
)