Middle
What do you use for state management and what would you choose in a new project?
sobes.tech AI
Answer from AI
For state management in the frontend, I often use Redux or MobX, depending on the complexity of the application. Redux is well-suited for large applications with a predictable data flow, while MobX is more reactive and less formal.
In a new project, I lean towards using React Context + useReducer for simple cases, and for more complex scenarios — Redux Toolkit, as it reduces boilerplate code and improves DX. I also consider Recoil or Zustand as lighter alternatives.
Example with Redux Toolkit:
import { configureStore, createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1,
decrement: state => state - 1
}
});
const store = configureStore({
reducer: {
counter: counterSlice.reducer
}
});
store.dispatch(counterSlice.actions.increment());
console.log(store.getState());