<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anuj Rajak</title>
    <description>The latest articles on DEV Community by Anuj Rajak (@anujrajak).</description>
    <link>https://dev.to/anujrajak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F532281%2F22748faa-66e7-4cf8-9595-8e5020971d79.jpg</url>
      <title>DEV Community: Anuj Rajak</title>
      <link>https://dev.to/anujrajak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anujrajak"/>
    <language>en</language>
    <item>
      <title>The Dynamic Trio: React, Redux Toolkit, and Redux Saga 🚀</title>
      <dc:creator>Anuj Rajak</dc:creator>
      <pubDate>Tue, 27 Aug 2024 10:58:30 +0000</pubDate>
      <link>https://dev.to/anujrajak/the-dynamic-trio-react-redux-toolkit-and-redux-saga-4mj2</link>
      <guid>https://dev.to/anujrajak/the-dynamic-trio-react-redux-toolkit-and-redux-saga-4mj2</guid>
      <description>&lt;p&gt;React, Redux Toolkit, and Redux Saga form a powerful trio for building complex, state-managed web applications. 🌟 In this post, we’ll explore how to integrate these technologies step-by-step, making your app efficient and maintainable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Setting Up Your React App 🎨
&lt;/h3&gt;

&lt;p&gt;First, create a new React application using Create React App. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
cd my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Installing Redux Toolkit and Redux Saga 🗄️
&lt;/h3&gt;

&lt;p&gt;Next, install Redux Toolkit and Redux Saga by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @reduxjs/toolkit react-redux redux-saga
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Setting Up Redux Toolkit 📦
&lt;/h3&gt;

&lt;p&gt;Create a new folder called store in the src directory, and inside it, create a file named store.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/store.js
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from './rootReducer'; // Import your root reducer
import createSagaMiddleware from 'redux-saga';
import rootSaga from './sagas'; // Import your root saga

const sagaMiddleware = createSagaMiddleware();

const store = configureStore({
  reducer: rootReducer,
  middleware: [sagaMiddleware],
});

sagaMiddleware.run(rootSaga);

export default store;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Creating Your Reducers 🌀
&lt;/h3&gt;

&lt;p&gt;Create a counterSlice.js file in the store folder to define your counter reducer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 },
  reducers: {
    increment: (state) =&amp;gt; {
      state.value += 1;
    },
    decrement: (state) =&amp;gt; {
      state.value -= 1;
    },
  },
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Creating Your Root Reducer 🌀
&lt;/h3&gt;

&lt;p&gt;In your rootReducer.js file, import the counter reducer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/rootReducer.js
import { combineReducers } from 'redux';
import counterReducer from './counterSlice'; // Import your counter reducer

const rootReducer = combineReducers({
  counter: counterReducer,
});

export default rootReducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Creating a Simple Saga 🕸️
&lt;/h3&gt;

&lt;p&gt;Next, create a sagas.js file in the store folder. Here’s a simple saga that logs a message when the counter is incremented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/store/sagas.js
import { takeEvery } from 'redux-saga/effects';
import { increment } from './counterSlice';

function* logIncrement() {
  console.log('Counter incremented!');
}

function* rootSaga() {
  yield takeEvery(increment.type, logIncrement);
}

export default rootSaga;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 7: Connecting Redux to Your React App 🔗
&lt;/h3&gt;

&lt;p&gt;Now, connect Redux to your React application. Open your index.js file and wrap your App component with the Provider from react-redux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store/store'; // Import your store
import App from './App';

ReactDOM.render(
  &amp;lt;Provider store={store}&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;,
  document.getElementById('root')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 8: Creating a Simple Component with Redux 📲
&lt;/h3&gt;

&lt;p&gt;Now, let’s create a simple component that uses Redux state. Create a file called Counter.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/Counter.js
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './store/counterSlice'; // Import actions

const Counter = () =&amp;gt; {
  const count = useSelector((state) =&amp;gt; state.counter.value); // Access your state
  const dispatch = useDispatch();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Count: {count}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch(increment())}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch(decrement())}&amp;gt;Decrement&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 9: Adding Your Component to the App 🌈
&lt;/h3&gt;

&lt;p&gt;Finally, add your Counter component to the App.js file:&lt;br&gt;
javascript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/App.js
import React from 'react';
import Counter from './Counter';

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Welcome to My App!&amp;lt;/h1&amp;gt;
      &amp;lt;Counter /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Putting It All Together 🎉
&lt;/h3&gt;

&lt;p&gt;By combining React, Redux Toolkit, and Redux Saga, you create a powerful application that handles complex state management seamlessly. 💪 React provides the UI, Redux Toolkit manages the state, and Redux Saga takes care of side effects. &lt;/p&gt;

&lt;p&gt;🕹️ This integration results in a smooth and efficient user experience, making your app reliable and easy to maintain. 🎢&lt;br&gt;
So, what are you waiting for? Start building your next amazing web app with React, Redux Toolkit, and Redux Saga today! 🚀&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>reduxtoolkit</category>
      <category>reduxsaga</category>
    </item>
  </channel>
</rss>
