Redux Toolkit: Create an action that dispatches multiple other actions
6 March 2023 (Updated 6 March 2023)
import { createAsyncThunk } from '@reduxjs/toolkit';
export const fetchUserData = createAsyncThunk(
'user/fetchData',
async (userId, thunkAPI) => {
const response = await fetch(`https://jsonplaceholder.typicode.com/users/${userId}`);
const data = await response.json();
// Dispatch multiple actions
thunkAPI.dispatch(actions.setUserName(data.name));
thunkAPI.dispatch(actions.setUserEmail(data.email));
return data;
}
);
Tagged:
Redux
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment