Complete the code to create a Redux slice with a name.
import { createSlice } from '@reduxjs/toolkit'; const counterSlice = createSlice({ name: '[1]', initialState: 0, reducers: {} });
The name property defines the slice name, which is usually descriptive like 'counter'.
Complete the code to add a reducer that increments the state by 1.
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: (state) => state [1] 1
}
});The reducer returns the new state by adding 1 to the current state using the '+' operator.
Fix the error in the reducer to correctly update the state by incrementing it.
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: (state) => {
state [1] 1;
}
}
});When using Immer in Redux Toolkit, you can mutate the state directly with state += 1;. The correct syntax is state += 1;.
Fill both blanks to export the action and reducer from the slice.
export const { [1] } = counterSlice.actions;
export default counterSlice.[2];The action increment is exported from counterSlice.actions. The default export is the reducer property.
Fill all three blanks to create a Redux store with the counter reducer.
import { configureStore } from '@reduxjs/toolkit'; import counterReducer from './counterSlice'; const store = configureStore({ reducer: { [1]: [2] } }); export default [3];
The store is configured with a reducer object where the key is the slice name counter and the value is the imported counterReducer. The store is exported as default.