Interview question
How do you use Redux Thunk for async actions? Redux Thunk से async actions कैसे use करें?
Answer
// Redux Thunk - middleware for async operations
function fetchUser(userId) {
return dispatch => {
dispatch({ type: 'FETCH_USER_REQUEST' });
fetch(`/api/users/${userId}`)
.then(res => res.json())
.then(data => {
dispatch({ type: 'FETCH_USER_SUCCESS', payload: data });
})
.catch(error => {
dispatch({ type: 'FETCH_USER_ERROR', payload: error });
});
};
}
// Using
store.dispatch(fetchUser(1));Redux Thunk async operations के लिए।
Dispatch function return करो।Was this answer clear?