diff --git a/Productivity/Programming/React/Debug Redux.md b/Productivity/Programming/React/Debug Redux.md new file mode 100644 index 0000000..1f4ed48 --- /dev/null +++ b/Productivity/Programming/React/Debug Redux.md @@ -0,0 +1,119 @@ +# Debugging React and Redux Applications: A Step-by-Step Guide + +Debugging is an essential skill for building robust applications. Whether you’re working with React components or managing application state with Redux, having a reliable debugging process will save you time and effort. In this guide, we’ll explore effective ways to debug both React variables and Redux state in the browser. + +--- + +## **Debugging Redux State in the Browser** + +### **Step 1: Configure Your Redux Store** +To enable debugging, modify your Redux store configuration to expose the store in development mode: + +```javascript +import { configureStore } from '@reduxjs/toolkit'; +import rootReducer from './reducers'; +import { composeWithDevTools } from '@redux-devtools/extension'; + +const store = configureStore({ + reducer: rootReducer, +}); + +if (process.env.NODE_ENV === 'development') { + window.store = store; +} + +export default store; +``` + +### **Step 2: View Redux State** +Once your store is exposed, you can inspect the Redux state directly in the browser console: + +1. Open your application in the browser. +2. Open the developer tools (usually F12 or right-click > "Inspect"). +3. Navigate to the "Console" tab. +4. Type `window.store.getState()` and press Enter. This will log the entire Redux state object. + +To inspect specific slices of state, drill down into the object. For example: +```javascript +window.store.getState().mySliceName; +``` + +### **Step 3: Use Redux DevTools Extension** +The Redux DevTools browser extension provides a structured and user-friendly interface for inspecting Redux state and actions. + +- Install the Redux DevTools extension for your browser. +- Open the "Redux" tab in developer tools. +- View dispatched actions, state changes, and time travel through your app's state. + +--- + +## **Debugging React Variables** + +### **1. Using `console.log()`** +The simplest way to debug variables is to log them in the console: +```javascript +import React, { useState } from 'react'; + +function ExampleComponent() { + const [count, setCount] = useState(0); + + console.log('Count:', count); + + return ( +
Count: {count}
+ +