createSlice kết hợp action creators + reducer trong cùng một file, dùng Immer nên có thể mutate state trực tiếp.
js
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
setUser(state, action) { state.name = action.payload; }
}
});
export const { setUser } = userSlice.actions;
export default userSlice.reducer;createSlice combines action creators and a reducer in one place: createSlice({ name: 'user', initialState, reducers: { setUser(state, action) { state.name = action.payload } } }).
It auto-generates action type strings, uses Immer under the hood so you can write mutating-style updates, and exports both action creators and the reducer.