class A {
constructor( props ){
this.props.getItems();
}
render(){
return( true )
}
}
export const getItems = () => {
return async dispatch => {
function onSuccess(success) {
dispatch({ type: UPDATE_AJAX_PARAMS, payload: success.data});
}
function onError(error) {
dispatch({ type: UPDATE_AJAX_PARAMS, error });
}
try {
const success = await axios.post( 'http://....');
return onSuccess(success);
} catch (error) {
return onError(error);
}
}
const someMiddleware = store => next => action => {
if(action.type === 'Нужный_вам_экшн'){
loadData(store.dispatch);
}
}
const loadData = async(dispatch) =>{
const resultData = await fetchDataFromServer();
dispatch(actions.saveData(resultData));
}
export const getItems= () => {
return async dispatch => {
await axios.get(baseUrl + '/items', {
params: {
token: someToken
}
})
.then(res => {
dispatch({type: 'GET_ITEM', data: res.data})
dispatch({ type: 'FETCH_SUCCESS' });
})
.catch(error => {
dispatch({ type: 'FETCH_FAILED', message: error });
});
}
}