Redux Toolkit vs Core Redux for React Applications

💡React Interview Question💡 Why to use Redux toolkit instead of core redux? Answer: Redux toolkit is preferred instead of core redux for new React applications, because it helps to avoid writing boilerplate code. Also we get the following benefits with redux toolkit: ✅ No need to manually add redux devtool configuration in the code, as the configuration comes in-built with the redux toolkit. ✅ No need to separately install the redux-thunk package to make async API calls, because it also comes in-built with the redux toolkit. ✅ Redux toolkit helps to avoid complex manipulation and write less code So, If you have a redux state like this: const initialState = { profile: { name: 'Mike', location: { state: 'NY', city: 'NY' } } } then If you want to update the city name to 'Amsterdam' using 𝗰𝗼𝗿𝗲 𝗿𝗲𝗱𝘂𝘅, we need to write reducer code like this: const userReducer = (state = initialState, action) => { ... case UPDATE_PROFILE_CITY: return { ...state, profile: { ...state.profile, location: { ...state.profile.location, city: action.payload, }, }, }; ... } However, with the redux toolkit you can write code to update in a single line like this: const usersSlice = createSlice({ ... reducers: { updateProfileCity(state, action) { state .profile .location .city = action.payload; }, } }); So with redux toolkit, you don't need to return a new state object with modifications, instead, you can directly update any property of the state because createSlice uses the immer.js library behind the scenes to achieve the immutability. 𝗙𝗼𝗿 𝗺𝗼𝗿𝗲 𝘀𝘂𝗰𝗵 𝘂𝘀𝗲𝗳𝘂𝗹 𝗰𝗼𝗻𝘁𝗲𝗻𝘁, 𝗱𝗼𝗻'𝘁 𝗳𝗼𝗿𝗴𝗲𝘁 𝘁𝗼 𝗳𝗼𝗹𝗹𝗼𝘄 𝗺𝗲. #javascript #reactjs #redux #interviewquestions #webdevelopment

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories