Answer the question
In order to leave comments, you need to log in
If there are no asynchronous requests in the action, is it necessary to use middleware?
Greetings) Is it necessary to use middleware if there are no asynchronous requests in the action... but you need to do some calculations, for example, or something else...? Or is middleware only needed for asynchronous actions ?
1) Write like this?
const action = (a, b, c) => {
return dispatch => {
const result = (a,b,c) => a + b + c;
dispatch(dispatchResult(result))
}
};
const dispatchResult = (result) => {
return {
type: ACTION_TYPE,
payload: {
result: result
}
};
}
const action = (a, b, c, d) => {
const result = (a,b,c) => a + b + c;
return {
type: ACTION_TYPE,
payload: {
result: result
}
};
};
Answer the question
In order to leave comments, you need to log in
For asynchronous actions, use the redux-thunk middleware, with it you can write this kind of code:
const fetchData = () => {
return async (dispatch) => {
// start fetching
dispatch({type: FETCH_DATA_START});
try {
const {data} = await api.fetch('/data');
dispatch({type: FETCH_DATA_SUCCESS, data});
} catch (err) {
dispatch({type: FETCH_DATA_ERROR, error: err});
}
}
}
count directly in the component
import { sum } from 'lodash'
...
// где-то в методе какого-то компонента
this.props.action(sum([a, b, c]))
...
const action = payload => ({
type: ACTION_TYPE,
payload
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question