This project is a simple React application that utilizes Redux for state management, specifically designed to demonstrate the core concepts of the React Redux Toolkit. The application features a counter that allows users to increment, decrement, and reset the count. It is built using Vite as the development tool, which provides a fast and efficient environment for modern web applications. The project is structured following best practices, ensuring clarity and maintainability.
Purpose:
This file configures the Redux store, which manages the application state and makes it accessible to all components.
Key Code Explanation:
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counter/counterSlice.js";
export const store = configureStore({
reducer: {
counter: counterReducer,
}
})- Imports the
configureStorefunction from Redux Toolkit and thecounterReducerfromcounterSlice.js. - Creates a Redux store with a single reducer for managing the counter's state.
Purpose:
This file defines the counterSlice, which contains the reducer and actions for managing the counter's state.
Key Code Explanation:
import { createSlice } from '@reduxjs/toolkit';
const initialState = { value: 0 }; // Ensure you have an initial state defined
export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
state.value++;
},
decrement: (state) => {
state.value--;
},
reset: (state) => {
state.value = 0;
},
}
});
// Export the actions
export const { increment, decrement, reset } = counterSlice.actions;
// Export the reducer
export default counterSlice.reducer;- Imports
createSlicefrom Redux Toolkit to simplify reducer creation. - Defines an initial state with a
valueproperty set to0. - Defines three reducers:
increment: Increases thevalueby 1.decrement: Decreases thevalueby 1.reset: Resets thevalueto0.
- Exports the actions (
increment,decrement,reset) for dispatching from components. - Exports the reducer for integration into the Redux store.
Purpose:
This is the entry point of the React application where the root component is rendered into the DOM. It integrates the Redux store into the application using the Provider component.
Key Code Explanation:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import { store } from './app/store.js'
import { Provider } from 'react-redux'
createRoot(document.getElementById('root')).render(
<StrictMode>
<Provider store={store}>
<App />
</Provider>
</StrictMode>,
)- Imports necessary modules, including React and Redux components.
- Renders the
Appcomponent into the DOM, wrapped inProviderto provide access to the Redux store. - Uses
StrictModeto help identify potential problems during development.
Purpose:
The App component serves as the main interface for the counter functionality, showcasing how to interact with the Redux store using hooks.
Key Code Explanation:
import { useDispatch, useSelector } from 'react-redux'
import './App.css'
import { increment, decrement, reset } from './app/counter/counterSlice'
function App() {
const dispatch = useDispatch();
const count = useSelector((state) => state.counter.value)
return (
<>
<h1>Learn About React Redux Toolkit</h1>
<div>
<h1>{count}</h1>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
<button onClick={() => dispatch(reset())}>Reset</button>
</div>
</>
)
}
export default App- Uses
useDispatchto dispatch actions (increment,decrement, andreset) to the Redux store. - Uses
useSelectorto access the current counter value from the Redux state. - Renders the counter and three buttons for user interaction.
This React application demonstrates the use of Redux for state management through a simple counter interface. The project uses Vite for fast development and efficient builds, while the clear structure of JavaScript and JSX files ensures maintainability. The counterSlice.js file highlights the simplicity and power of Redux Toolkit for managing state in modern web applications.