Junior — Middle
Is it possible to use the Redux management library without a React application associated with it?
sobes.tech AI
Answer from AI
Yes, Redux can be used independently of React. Redux is a state management library that does not depend on React and can be applied in any JavaScript application, including Node.js. For example, you can use Redux for centralized state management in a server application or in any other environment where predictable state change logic is needed.
Here's a simple example of using Redux without React:
const { createStore } = require('redux');
// Reducer — a function describing how the state changes
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
const store = createStore(counter);
store.subscribe(() => console.log(store.getState()));
store.dispatch({ type: 'INCREMENT' }); // 1
store.dispatch({ type: 'INCREMENT' }); // 2
store.dispatch({ type: 'DECREMENT' }); // 1
Thus, Redux is a universal tool for state management, not tied to React.