我正在使用redux从我的在线api保存json文件,并使用map以堆栈格式显示它。现在我想为从redux接收到的json中的每篇文章添加视图,优先级和收藏夹,并根据每次查看卡片时更改的优先级。
export const apiMiddleware = store => next => action => {
// Pass all actions through by default
next(action);
switch (action.type) {
// In case we receive an action to send an API request
case 'GET_DATA':
// Dispatch GET_DATA_LOADING to update loading state
store.dispatch({type: 'GET_DATA_LOADING'});
// Make API call and dispatch appropriate actions when done
fetch(`https://news119.herokuapp.com/getData`)
.then(response => response.json())
.then(data => next({
type: 'GET_DATA_RECEIVED',
data
}))
.catch(error => next({
type: 'GET_DATA_ERROR',
error
}));
break;
// Do nothing if the action does not interest us
default:
break;
}
};
export const reducer = (state = { movies: [], loading: true }, action) => {
switch (action.type) {
case 'GET_DATA_LOADING':
return {
...state, // keep the existing state,
loading: true, // but change loading to true
};
case 'GET_DATA_RECEIVED':
return {
loading: false, // set loading to false
data: action.data.data, // update data array with reponse data
};
case 'GET_DATA_ERROR':
return state;
default:
return state;
}
};
return this.state.dataSource.map((item,i)=>{
整个地图功能确实很大,但这就是我目前正在映射的方式