What libraries did you use for state management?
sobes.tech AI
Answer from AI
For state management in the frontend, I used several popular libraries:
-
Redux — a classic library for managing global state in React applications. It allows centralized storage of state and management through actions and reducers.
-
MobX — a more reactive approach to state management, based on observable data and automatic component updates.
-
React Context API — a built-in React mechanism for passing state through the component tree without props.
-
Recoil — a modern library from Facebook for state management with a convenient API and support for atomic states.
The choice depends on the complexity of the application and architectural requirements. For example, small projects often suffice with Context API, while larger ones may require Redux or MobX.
Example of using Redux:
import { createStore } from 'redux';
const initialState = { count: 0 };
function reducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
default:
return state;
}
}
const store = createStore(reducer);
store.dispatch({ type: 'INCREMENT' });
console.log(store.getState()); // { count: 1 }